我知道已经有关于这个问题的话题,但所有的解决方案都不适合我。
我希望我的菜单在用户滚动时固定在我的页面顶部。
我找到了一个脚本并根据我的需要对其进行了调整:
var win = $(document.body);
var fxel = $('#stickynav');
var eloffset = $('#stickynav').offset().top;
console.log(win.scrollTop());
win.scroll(function() {
if (eloffset < win.scrollTop()) {
console.log('fixed');
fxel.addClass("fixed");
} else {
console.log(eloffset + ' != ' + win.scrollTop());
fxel.removeClass("fixed");
}
});
它适用于 firefox 和 IE8,但不适用于 chrome,win.scrollTop() 总是返回 0。
我尝试了一切 win = $(document)
、$(window)
和 $('body, html')
,它总是返回 0 或什么都不返回。
有人可以帮帮我吗?
最佳答案
我正在我的一个项目中使用它,它可能适合你。
//window events
$(window).on('scroll', function(){
that = $(this);
if(that.scrollTop()>break_point){
//YOUR CODE
}
});
更新
我已经解决了您的问题。 首先尝试更新你的 jQuery,如果问题仍然存在尝试使用这个: 我已经在您的页面上对此进行了测试,它似乎运行良好。
$(window).on('scroll', function(){
that = $(this);
//because you are wrapping all contents
var offsetTop = Math.abs($('.wrapper-1').offset().top);
if(offsetTop>break_point){
//CODE HERE
}
});
真诚地希望它有效。干杯
关于javascript - document.body scrollTop 在 chrome 中返回 0 但在 firefox 中不返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27981065/