Eri*_*rom 0 javascript jquery infinite-scroll
我正在为一个页面制作一个无限滚动功能,并且遇到了一个问题,即该$( window ).scroll()函数似乎正在触发两次.我在stackoverflow上看到了类似的问题,但它们似乎与快速滚动相关,并且没有适当的位置来检查请求已经发送的天气.这是我现在正在使用的代码,我想这里有一些我很想念的东西.
var loading = false;
var scrollcount = 0;
$( window ).scroll( function(){
scrollcount++;
var scrolling = $( document ).scrollTop();
var position = $( "#ajax-load" ).offset();
if( scrolling + $( window ).height() > position.top - 200 && loading == false ){
console.log( loading ); //returns 2 falses
console.log( scrollcount ); //returns 2 of the same number
console.log( $( document ).scrollTop() ); //returns same number
loading = true;
$( "#ajax-load" ).css( "display", "block" );
$.get( "url/to/my/script", info )
.done( function( data ){
//here's where the items are appended to the document
setTimeout( function(){
loading = false;
}, 5000 );
});
}
});
Run Code Online (Sandbox Code Playgroud)
当我注销scrollcount变量并从scrollTop()返回时,我得到两次相同的数字似乎告诉我事件实际上是由于某种原因同时触发了两次.似乎如果它一次又一次地发射两次,那么加载变量将被设置为假而第二次不会发射.就像我说的那样,我想这是一件非常简单的东西,我很想念.谢谢你的帮助!
小智 5
我的第一个猜测是你有两次应用事件监听器,无论这个代码是什么.
尝试添加$(window).unbind('scroll'); 在$(窗口)之前.scroll
加载变量在两次触发时为false,因为在异步调用之后,在超时时设置为false
| 归档时间: |
|
| 查看次数: |
3902 次 |
| 最近记录: |