2014-01-19 128 views
14

我想檢測div滾動。此代碼所做的是檢測整個窗口滾動:檢測div滾動jquery

$(document).ready(function() { 
var track_load = 0; //total loaded record group(s) 
var loading = false; //to prevents multipal ajax loads 
var total_groups = <?php echo $total_groups; ?>; //total record group(s) 

$('#results').load("autoload_process.php", {'group_no':track_load}, function() {track_load++;}); //load first group 

$(window).scroll(function() { //detect page scroll 

    if($(window).scrollTop() + $(window).height() == $(document).height()) //user scrolled to bottom of the page? 
    { 

     if(track_load <= total_groups && loading==false) //there's more data to load 
     { 
      loading = true; //prevent further ajax loading 
      $('.animation_image').show(); //show loading image 

      //load data from the server using a HTTP POST request 
      $.post('autoload_process.php',{'group_no': track_load}, function(data){ 

       $("#results").append(data); //append received data into the element 

       //hide loading image 
       $('.animation_image').hide(); //hide loading image once data is received 

       track_load++; //loaded group increment 
       loading = false; 

      }).fail(function(xhr, ajaxOptions, thrownError) { //any errors? 

       alert(thrownError); //alert with HTTP error 
       $('.animation_image').hide(); //hide loading image 
       loading = false; 

      }); 

     } 
    } 
}); 

});

這是我的HTML代碼。

<div id="scrollingbox"> 
<ol id="results"> 
</ol> 
<div class="animation_image" style="display:none" align="center"><img src="ajax-loader.gif"></div> 
</div> 

我使用了div ID,但它不輸出任何內容,而是輸出任何內容。

回答

30

不知道,但你可以參考由ID,等級等方面的DIV在滾動()函數:

下面是一個簡單的jsfiddle:http://jsfiddle.net/collabcoders/v2RbN/

$(".box").scroll(function() { //.box is the class of the div 
    $("span").css("display", "inline").fadeOut("slow"); 
}); 

更新時間:http://jsfiddle.net/collabcoders/v2RbN/1/

$("span").hide(); 

$(".box").scroll(function() { 
    if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) { 
     $("span").show();  
    } else { 
     $("span").hide(); 
    } 
}); 
+0

我修改了我的問題先生,看看http://stackoverflow.com/questions/21212051/jquery-find-if-div-reached-the-bottom-on-scroll – user3196424

+0

確定更新了代碼 – johnnyarguelles

+2

它的工作,謝謝你很多!你從我校的總裁兼副總裁拯救了我的生命! – user3196424