2016-10-27 45 views
0

我有一個滑動菜單:滑動菜單可疊加文字時關閉

@font-face { 
 
    font-family: ErasBold; 
 
    src: url('fonts/erasbd.TTF'); /* Chrome, Opera, Firefox */ 
 
} 
 

 
@font-face{ 
 
    font-family: ErasDemi; 
 
    src: url('fonts/erasdemi.TTF'); 
 
} 
 
@font-face{ 
 
    font-family: ErasLight; 
 
    src: url('fonts/eraslight.TTF'); 
 
} 
 
body { 
 
    background-color: #EDEDED; 
 
    width: 90%; 
 
    min-height: 100%; 
 
    /*border-color: #000000; !INCASE YOU WOULD LIKE TO SEE WHERE THE BODY IS LOCATED ON THE SCREEN. USE FOR DEBUGGING VARIOUS DEVICES! 
 
    border-style: outset;*/ 
 
    margin: 0 auto; 
 
    transition: 1s; 
 
} 
 

 

 
#header { 
 
    background-color: #000000; 
 
    position: fixed; 
 
    height: 95px; 
 
    width: 100%; 
 
    top: 0 
 
    left: 0; 
 
    right: 0; 
 
} 
 

 
#mainpage { 
 
    background-color: #EDEDED; 
 
    height: 82%; 
 
    width: 100%; 
 
    margin: 0 auto -4em; 
 
    position: relative; 
 
    padding: none; 
 
    overflow: auto; 
 
} 
 

 
#footer{ 
 
    height: 80px; 
 
    width: 100%; 
 
    background-color: #000000; 
 
    position: fixed; 
 
    bottom: 0; 
 
    left: 0; 
 
    right: 0; 
 

 

 
} 
 

 
#logo{ 
 
    height: 100%; 
 
    width: auto; 
 
} 
 

 

 
.sideNav{ 
 
    height: 100%; 
 
    width: 0; 
 
    position: fixed; 
 
    z-index: 1; 
 
    top: 0; 
 
    right: 0; 
 
    background-color: #111; 
 
    overflow-x: hidden; 
 
    padding-top: 60px; 
 
    transition: 0.5s; 
 
} 
 

 
.sideNav a{ 
 
    padding: 8px 8px 8px 32px; 
 
    text-decoration: none; 
 
    font-size: 27px; 
 
    color: #818181; 
 
    display: block; 
 
    transition: 0.3s; 
 
    font-family: ErasLight; 
 
} 
 

 

 
.sideNav a:hover, .offcanvas a:focus{ 
 
    color: #f1f1f1; 
 
} \t 
 

 
.sideNav .closeButton{ 
 
    position: absolute; 
 
    top: 0; 
 
    right: 25px; 
 
    font-size: 40px; 
 
    margin-left: 50px; 
 
} 
 

 
@media screen and (max-height: 450px){ 
 
    .sideNav {padding-top: 15px;} 
 
    .sideNav a {font-size: 18px;} 
 
} 
 

 
#menuStuff{ 
 
    color: white; 
 
    float: right; 
 
    font-size: 30px; 
 
    font-family: ErasLight; 
 
    text-decoration: none; 
 
    margin-top: 20px; 
 
    margin-right: 20px; 
 
} 
 

 
#menuStuff .hover { 
 
    color: #FFFFFF; 
 
    text-decoration: none; 
 
}
<body> \t \t 
 

 
    <div id="header"> 
 

 
    <a href="index.html"> <img src="images/logo.png" id="logo"> </a> 
 

 
    <div id="mySideNav" class="sideNav"> 
 
     <a href="javascript:void(0)" class="closeButton" onclick="closeNav()">&times;</a> 
 
     <a href="index.html">Home</a> 
 
     <a href="">About Me</a> 
 
     <a href="">My Work</a> 
 
    </div> 
 

 
    <a href="#"><span onclick="openNav()" id="menuStuff">Menu &#9776;</span></a> 
 

 
    <script> 
 
     function openNav() { 
 
     document.getElementById("mySideNav").style.width = "250px"; 
 
     document.body.style.backgroundColor = "rgba(0,0,0,0.4)"; 
 
     } 
 

 
     function closeNav() { 
 
     document.getElementById("mySideNav").style.width = "0px"; 
 
     document.body.style.backgroundColor = "white"; 
 
     } 
 
    </script> 
 

 
    </div> 
 

 
    <div id="mainpage"> 
 

 
    </div> 
 

 

 
    <div id="footer"> 
 

 
    </div> 
 
</body>

http://codepen.io/dangranger/pen/WGWqvm 

而當你點擊「X」關閉它,文本上的每個頂部堆疊其他當它向內移動時。我怎樣才能做到這一點,以便它保持原狀,然後滑出去?

(對不起,結構差,我努力解釋)

+0

你使用什麼瀏覽器?它似乎在我的鉻合作 – alexc

回答

1

那是因爲你改變寬度。即使.sideNavwidth: 1px(在動畫開始時),x仍然存在。

可以代替動畫的位置:

注:頁面的注意,而不是更改腳本的風格,我只需撥動類open,並控制在CSS樣式。我認爲這是一個更好的方法。

function openNav() { 
 
    document.getElementById("mySideNav").classList.add('open'); 
 
    document.body.style.backgroundColor = "rgba(0,0,0,0.4)"; 
 
} 
 

 
function closeNav() { 
 
    document.getElementById("mySideNav").classList.remove('open'); 
 
    document.body.style.backgroundColor = "white"; 
 
}
@font-face { 
 
    font-family: ErasBold; 
 
    src: url('fonts/erasbd.TTF'); /* Chrome, Opera, Firefox */ 
 
} 
 

 
@font-face{ 
 
    font-family: ErasDemi; 
 
    src: url('fonts/erasdemi.TTF'); 
 
} 
 
@font-face{ 
 
    font-family: ErasLight; 
 
    src: url('fonts/eraslight.TTF'); 
 
} 
 
body { 
 
    background-color: #EDEDED; 
 
    width: 90%; 
 
    min-height: 100%; 
 
    /*border-color: #000000; !INCASE YOU WOULD LIKE TO SEE WHERE THE BODY IS LOCATED ON THE SCREEN. USE FOR DEBUGGING VARIOUS DEVICES! 
 
    border-style: outset;*/ 
 
    margin: 0 auto; 
 
    transition: 1s; 
 
} 
 

 

 
#header { 
 
    background-color: #000000; 
 
    position: fixed; 
 
    height: 95px; 
 
    width: 100%; 
 
    top: 0 
 
    left: 0; 
 
    right: 0; 
 
} 
 

 
#mainpage { 
 
    background-color: #EDEDED; 
 
    height: 82%; 
 
    width: 100%; 
 
    margin: 0 auto -4em; 
 
    position: relative; 
 
    padding: none; 
 
    overflow: auto; 
 
} 
 

 
#footer{ 
 
    height: 80px; 
 
    width: 100%; 
 
    background-color: #000000; 
 
    position: fixed; 
 
    bottom: 0; 
 
    left: 0; 
 
    right: 0; 
 

 

 
} 
 

 
#logo{ 
 
    height: 100%; 
 
    width: auto; 
 
} 
 

 

 
.sideNav{ 
 
    height: 100%; 
 
    width: 250px; 
 
    position: fixed; 
 
    z-index: 1; 
 
    top: 0; 
 
    right: 0; 
 
    background-color: #111; 
 
    overflow-x: hidden; 
 
    padding-top: 60px; 
 
    transition: 0.5s; 
 
    transform: translateX(250px); 
 
} 
 

 
.sideNav.open { 
 
    transform: translateX(0); 
 
} 
 

 
.sideNav a{ 
 
    padding: 8px 8px 8px 32px; 
 
    text-decoration: none; 
 
    font-size: 27px; 
 
    color: #818181; 
 
    display: block; 
 
    transition: 0.3s; 
 
    font-family: ErasLight; 
 
} 
 

 

 
.sideNav a:hover, .offcanvas a:focus{ 
 
    color: #f1f1f1; 
 
} \t 
 

 
.sideNav .closeButton{ 
 
    position: absolute; 
 
    top: 0; 
 
    right: 25px; 
 
    font-size: 40px; 
 
    margin-left: 50px; 
 
} 
 

 
@media screen and (max-height: 450px){ 
 
    .sideNav {padding-top: 15px;} 
 
    .sideNav a {font-size: 18px;} 
 
} 
 

 
#menuStuff{ 
 
    color: white; 
 
    float: right; 
 
    font-size: 30px; 
 
    font-family: ErasLight; 
 
    text-decoration: none; 
 
    margin-top: 20px; 
 
    margin-right: 20px; 
 
} 
 

 
#menuStuff .hover { 
 
    color: #FFFFFF; 
 
    text-decoration: none; 
 
}
<body> \t \t 
 

 
    <div id="header"> 
 

 
    <a href="index.html"> <img src="images/logo.png" id="logo"> </a> 
 

 
    <div id="mySideNav" class="sideNav"> 
 
     <a href="javascript:void(0)" class="closeButton" onclick="closeNav()">&times;</a> 
 
     <a href="index.html">Home</a> 
 
     <a href="">About Me</a> 
 
     <a href="">My Work</a> 
 
    </div> 
 

 
    <a href="#"><span onclick="openNav()" id="menuStuff">Menu &#9776;</span></a> 
 
    </div> 
 
    <div id="mainpage"> 
 

 
    </div> 
 
    <div id="footer"> 
 

 
    </div> 
 
</body>

+0

@丹你有沒有試過我的答案? –

+0

是的!對於遲交的道歉,我最近一直很忙 - 感謝它的工作絕對好! –

+0

@DanGranger真棒!你能[接受](http://stackoverflow.com/help/someone-answers)答案,這樣對其他人會有幫助嗎? –

0

它發生,因爲你正在修改的容器的寬度。

另一種方法是將容器本身滑入/滑出視口。這可以通過許多方式實現,通常使用right,margin-righttranslate(或者這幾天可能是translate3d)。