0
我有一個水平的粘性菜單,當用戶滾動時,它粘在頁面的頂部。 我有一個非常小的問題,但只是煩人。粘性菜單略有移動?
當頁面滾動時,整個菜單右移幾個像素。我希望它不會類似於這個網站http://www.welcomewebstudios.com/。
誰能幫
HTML
<div class="menu">
<ul>
<li><a href="#">About Me</a></li>
<li><a href="#">My Work</a></li>
<li><a href="#">Experience</a></li>
<li><a href="#">Contact Me</a></li>
</ul>
</div>
JQuery的
$(document).ready(function(){
var menu = document.querySelector('.menu');
var origOffsetY = menu.offsetTop;
function scroll() {
if ($(window).scrollTop() >= origOffsetY) {
$('.menu').addClass('sticky');
$('.content').addClass('menu-padding');
} else {
$('.menu').removeClass('sticky');
$('.content').removeClass('menu-padding');
}
}
document.onscroll = scroll;
});
CSS
* {font-family:arial; padding:0;}
.menu {
font:Verdana, Geneva, sans-serif;
border-style:groove;
border-width:1px;
border-color:#333;
background-color:#000;
font-size:20px;
height:50px;
line-height:30px;
width:100%;
text-align:center;
}
.content {margin-top:10px;}
.menu-padding {padding-top:40px;}
.content p {margin-bottom:20px;}
.sticky {position:fixed; top:0;}
li{ display:inline-block; margin:0 0 0 50px;}
感謝您的幫助,但是這使得它留在頂部所有的時間。我希望它在標題下,然後在用戶滾動時粘到頂端,但是我發現如果我只剩下了:0;它在滾動時略微向右移動。謝謝 – user3084397