0
我想在窗口向下滾動超過160像素時將菜單固定在頂部,但如果主體內容太短,它將變成無限循環,因爲如果向下滾動超過160像素,菜單將變成固定的,這意味着滾動高度將變成低於160像素,所以腳本會使菜單相對返回,如何解決這個問題。動態浮動菜單問題
HTML
<div id="header">header</div>
<div id="content">content</div>
的JavaScript
$(window).on('scroll', function() {
var scroll = $(window).scrollTop();
if (scroll > 160) {
$('#header').css('position', 'fixed');
} else {
$('#header').css('position', 'relative');
}
});
CSS
body {
margin: 0;
padding: 0;
}
#header {
width: 100%;
height: 60px;
background: black;
color: yellow;
position: relative;
padding: 6px;
}
#content {
width: 100%;
height: 780px;
background: gray;
}