這是我自己的實現。
我遇到問題overflow
在offcanvas菜單處於活動狀態時無法正常工作。我克服這個問題的方法是在html
,body
和#page
標籤上應用overflow: hidden
和position: fixed
兩個標籤,這會在除菜單本身之外的任何其他標籤上強制執行noscroll。
希望它可以幫助別人!
HTML:
<header id="masthead">
<button type="button" class="toggle">Toggle</button>
</header>
<nav id="offcanvas-menu">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
<div id="page">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus pellentesque erat vel massa porttitor, ac sagittis velit vehicula. Nunc iaculis sapien justo, ac viverra mi convallis et. Sed ac massa lacinia, varius neque et, varius mi. Integer in ligula vitae arcu mollis mollis ut a tortor. Fusce quis tellus fringilla, venenatis arcu posuere, suscipit libero. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Vivamus hendrerit sodales leo, ac pellentesque odio fermentum nec. Ut feugiat in tortor quis cursus. Praesent porttitor imperdiet orci. Morbi sed ultricies velit. Nulla maximus rhoncus congue.</p>
<p>Proin nec pharetra tortor, eu sollicitudin ipsum. Morbi turpis magna, feugiat ornare pretium a, tristique ac urna. Phasellus rutrum sem a turpis tincidunt viverra in eget odio. Mauris a nibh eget lacus volutpat lobortis. Sed convallis posuere nisl, id blandit est posuere a. Duis tempor euismod nunc, ut suscipit ligula feugiat in. Phasellus congue nibh non vulputate consectetur.</p>
</div>
LESS:
html.offcanvas-active,
html.offcanvas-active body
html.offcanvas-active #page {
overflow: hidden;
position: fixed;
}
html.offcanvas-active {
#masthead,
#page {
-webkit-transform: translate(-250px);
-moz-transform: translate(-250px);
transform: translate(-250px);
}
#offcanvas-menu {
right: 0;
}
}
body {
font-family: Arial, sans-serif;
}
#masthead {
.toggle {
float: right;
}
&:after {
visibility: hidden;
display: block;
content: "";
clear: both;
}
}
#masthead,
#page {
position: relative;
-webkit-transition: transform 1s ease-in-out;
-moz-transition: transform 1s ease-in-out;
transition: transform 1s ease-in-out;
}
#offcanvas-menu {
position: fixed;
top: 0; right: -250px;
width: 250px; height: 100%;
background-color: orange;
-webkit-transition: right 1s ease-in-out;
-moz-transition: right 1s ease-in-out;
transition: right 1s ease-in-out;
ul {
margin: 0; padding-left: 0;
list-style: none;
li {
a {
display: block;
padding: 10px 15px;
text-decoration: none;
color: #fff;
}
}
}
}
JS/jQuery的:
$(document).ready(function() {
$('#masthead').on('click', '.toggle', function() {
$('html').toggleClass('offcanvas-active');
});
});
因爲我已經運行到一個臭蟲消除後'。 noscr來自身體的「oll」,部分內容在iOS8上被切斷。在我看來,這是一個瀏覽器錯誤。 – Matijs 2014-12-11 14:23:21