javascript – 检测div滚动jquery

前端之家收集整理的这篇文章主要介绍了javascript – 检测div滚动jquery前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想检测div滚动.这个代码是检测整个窗口滚动:
  1. $(document).ready(function() {
  2. var track_load = 0; //total loaded record group(s)
  3. var loading = false; //to prevents multipal ajax loads
  4. var total_groups = <?PHP echo $total_groups; ?>; //total record group(s)
  5.  
  6. $('#results').load("autoload_process.PHP",{'group_no':track_load},function() {track_load++;}); //load first group
  7.  
  8. $(window).scroll(function() { //detect page scroll
  9.  
  10. if($(window).scrollTop() + $(window).height() == $(document).height()) //user scrolled to bottom of the page?
  11. {
  12.  
  13. if(track_load <= total_groups && loading==false) //there's more data to load
  14. {
  15. loading = true; //prevent further ajax loading
  16. $('.animation_image').show(); //show loading image
  17.  
  18. //load data from the server using a HTTP POST request
  19. $.post('autoload_process.PHP',{'group_no': track_load},function(data){
  20.  
  21. $("#results").append(data); //append received data into the element
  22.  
  23. //hide loading image
  24. $('.animation_image').hide(); //hide loading image once data is received
  25.  
  26. track_load++; //loaded group increment
  27. loading = false;
  28.  
  29. }).fail(function(xhr,ajaxOptions,thrownError) { //any errors?
  30.  
  31. alert(thrownError); //alert with HTTP error
  32. $('.animation_image').hide(); //hide loading image
  33. loading = false;
  34.  
  35. });
  36.  
  37. }
  38. }
  39. });

});

这是我的HTML代码.

  1. <div id="scrollingBox">
  2. <ol id="results">
  3. </ol>
  4. <div class="animation_image" style="display:none" align="center"><img src="ajax-loader.gif"></div>
  5. </div>

我使用了div ID,但是它不输出两个.

解决方法

不确定,但是你可以在scroll()函数中通过id,class等引用div:

这是一个简单的jsfiddle:http://jsfiddle.net/collabcoders/v2RbN/

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

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

  1. $("span").hide();
  2.  
  3. $(".Box").scroll(function() {
  4. if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
  5. $("span").show();
  6. } else {
  7. $("span").hide();
  8. }
  9. });

猜你在找的jQuery相关文章