jQuery: detecting reaching bottom of scroll doesn&

2019-01-08 23:29发布

So basically my problem is a seemingly simple one.

You can see it in action at http://furnace.howcode.com (please note that data is returned via Ajax, so if nothing happens give it a few moments!).

What's MEANT to happen, is in the second column when you reach the bottom of scrolling, the next 5 results are returned.

But what actually happens is it only returns the 5 results when you hit the TOP of the scroll area. Try it: scroll down, nothing happens. Scroll back up to the top, the results are returned.

What's going wrong?

Here's my code I'm using:

$('#col2').scroll(function(){
    if ($('#col2').scrollTop() == $('#col2').height() - $('#col2').height()){
       loadMore();
    }
});

loadMore(); is the function that gets the data and appends it.

So what's going wrong here? Thanks for your help!

标签: jquery scroll
8条回答
来,给爷笑一个
2楼-- · 2019-01-09 00:07

I found an alternative that works.

None of these answers worked for me (currently testing in FireFox 22.0), and after a lot of research I found, what seems to be, a much cleaner and straight forward solution.

Implemented solution:

function IsScrollbarAtBottom() {
    var documentHeight = $(document).height();
    var scrollDifference = $(window).height() + $(window).scrollTop();
    return (documentHeight == scrollDifference);
}

Resource: http://jquery.10927.n7.nabble.com/How-can-we-find-out-scrollbar-position-has-reached-at-the-bottom-in-js-td145336.html

Regards

查看更多
趁早两清
3楼-- · 2019-01-09 00:15

This has worked for me in the past.

var col = $('#col2');
col.scroll(function(){
   if (col.outerHeight() == (col.get(0).scrollHeight - col.scrollTop()))
       loadMore(); 
});

Here's a good explanation too.

查看更多
我命由我不由天
4楼-- · 2019-01-09 00:15

The solution that jordanstephens posted is on the right track. However, what you need is not the height of #col2, you need the height of the parent element of #col2.

Example:

$('#col2').scroll(function(){
  if ($('#col2').scrollTop() == $('#col2').parent().height()) {
    loadMore();
  }
}
查看更多
戒情不戒烟
5楼-- · 2019-01-09 00:15

@munch was closest, but if you have a border on the element, then you need to use .innerHeight() instead of .outerHeight() since the latter includes the border and .scrollTop() does not (.height() is also out if you have padding). Also, at least as of jQuery 1.7.1 and maybe earlier, I'd recommend using .prop('scrollHeight') instead of retrieving it directly from the DOM. I found some talk that the DOM scrollHeight is broken on IE 5-8 and the jQuery function abstracts that for me, at least on IE8.

var $col = $('#col2');
$col.scroll(function(){
if ($col.innerHeight() == $col.prop('scrollHeight') - $col.scrollTop())
    loadMore(); 
});
查看更多
啃猪蹄的小仙女
6楼-- · 2019-01-09 00:17

The following has been tested, and works fine:

$(window).scroll(function() {
  if ($(window).scrollTop() == $(document).height() - $(window).height()) {
    loadMore();
  }
});
查看更多
Ridiculous、
7楼-- · 2019-01-09 00:19

Your math is wrong, your saying if the scrollbar position is equal to the height of the column minus the height of the column (which equals zero) load more. that's why your loadMore() is called at the top of your col.

Try:

$('#col2').scroll(function(){
    if ($('#col2').scrollTop() == $('#col2').height()){
       loadMore();
    }
});
查看更多
登录 后发表回答