我需要翻译关于IT技术的内容。以下是需要翻译的内容:
我的无限滚动有问题。我希望在页面底部距离300像素之前执行加载更多功能。
我使用以下方法检测滚动:
$window.scroll(function(){
在这个函数中,我测试了以下内容:
if (($document.height() - $window.height()) == $window.scrollTop()) {
但这只适用于当我们在页脚时。
然后我测试了这个:
if ($(window).scrollTop() >= $(document).height() - $(window).height() - 300)
但是无限滚动在页面的300像素和底部之间运行多次。
之后,我只想测试这个:
if ($(window).scrollTop() == $(document).height() - $(window).height() - 300)
但是这个不起作用,我不理解。 你有什么想法吗?
非常感谢。
var $window = $(window);
和var $document = $(document);
,并在您的scroll()
中引用它。否则,jQuery
将在每个滚动事件上创建两个新对象。 - spiritdocument
和window
是两个根变量,我认为性能问题不会很大。请看一下这篇关于这个问题的SO帖子:https://dev59.com/0W7Xa4cB1Zd3GeqPpGCB 然而,我已经进行了编辑,以防我错了,并考虑到.scroll()
创建了太多的事件。 - Iceman