我有以下代码:页脚一旦用户滚动向上滑动到页面底部 - 闪烁
var footer = $('.footer'),
extra = 0;
// footer.css({ opacity: '0', display: 'block' });
$(window).scroll(function() {
var scrolledLength = ($(window).height() + extra) + $(window).scrollTop(),
documentHeight = $(document).height();
console.log('Scroll length: ' + scrolledLength + ' Document height: ' + documentHeight)
if(scrolledLength >= documentHeight) {
footer
.addClass('bottom')
// .slideUp();
.stop().animate({ bottom: '0', opacity: '1' }, 300);
}
else if (scrolledLength <= documentHeight && footer.hasClass('bottom')) {
footer
.removeClass('bottom')
// .slideDown();
.stop().animate({ bottom: '-100', opacity: '0' }, 300);
}
});
我的目标是到页脚一旦用户滚动显示在页面的底部。如果用户再次向上滚动,我希望页脚再次滑下。
现在我正在检查scrolledLength
对documentHeight
。然而,问题似乎是,一旦我们达到最低点,documentHeight
就会改变,因为页脚出现并扩展了文档。
我的代码确实有效,页脚不会再消失或任何东西,但它闪烁很多(然后最终平静下来),因为它正在重新计算。我怎么解决这个问题?我的代码中是否有错误?
店如果页脚是可见的一个布尔值,如果是,计算高度documenth高度 - footerHeight? –