无限滚动 JavaScript 未运行

Infinite scroll javascript not running

本文关键字:运行 JavaScript 滚动 无限      更新时间:2023-09-26

我正在尝试使用此问题中给出的代码向页面添加无限滚动功能,检查用户是否已滚动到底部,但当我滚动到页面底部时没有任何反应。这是代码,因此您不必点击链接:

<script type="text/javascript"> 
alert("popup!");    
$(window).scroll(function() {   
   if($(window).scrollTop() + $(window).height() > $(document).height() - 100) {
       alert("bottom!");
   }
});  
</script>

我在第一个警报中添加了检查它是否只是我的浏览器阻止警报,但它显示正常。服务器还安装了 JQuery 1.7.2 分钟,并且页面正确是砖石,所以我认为这不是安装问题。

在你回复我的评论后,你说你得到了

In the console tab I'm getting Uncaught ReferenceError: $ is not defined

我猜,你还没有在页面的标题中包含jQuery(这需要在每个页面的<head>中)

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script> 

这将告诉您JQuery是否已成功加载到您的页面上

if (typeof jQuery != 'undefined') {
    alert("jQuery library is loaded!");
}else{
    alert("jQuery library is not found!");
}

原文评论:

尝试将console.log($(window).height()+"<-current sctoll | target->"+($(document).height() - 100))放入该循环中,在Chrome中打开它,然后右键单击->元素检查器,打开控制台选项卡,这将告诉您每个循环的值是什么,应该可以帮助您诊断问题。如果未获得任何跟踪,则事件未触发

也许滚动事件使用您那里的语法正确触发,请尝试以下操作:

$(window).on('scroll', function () { 
  if ($(window).height() + $(window).scrollTop() >= $(document).height() - 100) {
    alert('do stuff');  
  }
});