2015-04-01 53 views
0

你好我使用下面的代碼行用戶之前解僱多餘的項目100像素的追加是在底部:無限滾動火災前下方

var height = window.innerHeight; 
if ($(window).scrollTop() + height + 100 >= $(document).height()) 

的問題是,很多時候有多個追加命令發送。如果用戶在底部之前達到100px,則執行該命令。隨着用戶不斷上下滾動,命令一次又一次地執行。在大多數瀏覽器非常頻繁

$(window).scroll(function() { 
    if ($(window).scrollTop() + height == $(document).height()) { 
     // anything 
    } 
} 

回答

1

滾動事件觸發:

我已經試過這一點。我猜想它會在第一次請求返回之前多次觸發。如果已經有一個在飛行中,您可能需要添加一個標誌來阻止請求。

var requesting = false; 

function renderPage() { /* ... */ } 

function makeRequest() { 
    if (!requesting) { 
    // Go ahead and fetch the next page. 
    requesting = true; 
    $.ajax(...).then(renderPage).always(function() { 
     requesting = false; 
    }); 
    } 
} 

$(window).on('scroll', function() { 
    if (/* scrolled to 100px above the bottom */) { 
    makeRequest(); 
    } 
}); 
1
I have a similar infinite scroll script running in my site. Try doing this: 
<script type="text/javascript"> 
$(document).ready(function(){ 
    // Trigger Funtion when browser scroll triggered 
    $(window).scroll(function(){ 
     if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10){ 
//do action here 
     } 
    }); 
}); 
</script>