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;
}