2013-05-07 106 views
-1

我找到了这段代码。当您滚动到整个页面的底部时它会起作用。 我想改变这段代码来检测div的底部。jquery:当滚动到div的底部时使代码工作

这是代码

$(window).data('ajaxready', true).scroll(function(){ 
if ($(window).data('ajaxready') == false) return; 
if($(window).scrollTop() == $(document).height() - $(window).height()) 
{ 
    var datastring=$(".passform").last().serialize(); 
    $('div#loadmoreajaxloader').show(); 
    $(window).data('ajaxready', false); 
    $.ajax({ 
    type: "POST", 
    url: "post_update.php", 
    data: datastring, 
    cache : false, 
    success: function(html) 
    { 
     if(html) 
     { 
      $("#update").append(html); 
      $('div#loadmoreajaxloader').hide(); 
     }else 
     { 
      $('div#loadmoreajaxloader').html('<center>No more posts to show.</center>'); 
     } 
     $(window).data('ajaxready', true); 
    } 
    }); 
}}); 

谢谢!

回答

1

你可以这样做:

获取的div像这样底部的当前位置:

var bottom = $('#myDiv').position().top + $('#myDiv').outerHeight(true); 

Comapre与$(window).scrollTop()

if($(window).scrollTop() == bottom) { 
    // We are the bottom of the div 
1

$(窗口).height( )会给你的视口高度,直到你可以滚动页面。

var vH = $(window).height(); 
$('#yourSelector').scroll(function() { 
    if ($(this).scrollTop() == vH) { 
     //Do ur work 
    } 
}); 
1

我的想法是把它倒过来基本上。

$(window).scroll(function() { 
    var targetDiv = $('#div1'); 

    //Get the Y position of the bottom of the div 
    var divBottom = (targetDiv.position().top + targetDiv.height); 

    //get the top position of the scroll, minus the bottom of the div 
    //and then minus the height of the window as the user can see that much 
    var invertedTop = ((divBottom - $(window).scrollTop()) - $(window).height()); 

    //Less than or = to means we are at the bottom of the div. 
    if (invertedTop <= 0) {console.log('You have reached the bottom of the target div');} 
     else {console.log('You have not reached the bottom of the target div yet.');} 
});