Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
130
rated 0 times [  135] [ 5]  / answers: 1 / hits: 58305  / 12 Years ago, mon, november 5, 2012, 12:00:00

I want an event to load more content when a user is scrolling and reaches nearly the bottom of the page, say about 100px from the bottom, the problem is that the events get fired every single time you scroll in that lower 100px of the page, so that's an obvious issue that cannot happen, for obvious reasons, so I am wondering how I can do this best, I have checked out other answers/questions on here, however the one solution that partially works doesn't fit what I need it to do, and when i try to change it so it will, it completely freezes my browser every time.
this is the question:
Check if a user has scrolled to the bottom



The answer I'm looking at is near the bottom, but here is the code he suggests:



$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() > $(document).height() - 100) {
$(window).unbind('scroll');
alert(near bottom!);
}
});


now like I said, this does work and prevents more than one event being fired, the problem is I need to have a endless amount of events fired, say i load 100 rows of information when you reach the bottom, but there are still 1500 more, i need it to repeat each time to load every amount of information (once you reach the bottom 100px part of the page each time)



so what I have tried to do is:



 function loadMore()
{
alert(More loaded);
$(window).bind('scroll');
}

$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() > $(document).height() - 100) {
$(window).unbind('scroll');
loadMore();
}
});


like I said, this freezes up my browser immediately every time, the binding part.


More From » jquery

 Answers
22

I was having this same problem too. I came across this link and it really helped (and worked)!



Update: I looked at the code and I think that when you rebind, you are missing the function to rebind to.



jsFiddle



 function loadMore()
{
console.log(More loaded);
$(body).append(<div>);
$(window).bind('scroll', bindScroll);
}

function bindScroll(){
if($(window).scrollTop() + $(window).height() > $(document).height() - 100) {
$(window).unbind('scroll');
loadMore();
}
}

$(window).scroll(bindScroll);

[#82177] Sunday, November 4, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
monetm

Total Points: 615
Total Questions: 103
Total Answers: 119

Location: Finland
Member since Fri, Oct 21, 2022
2 Years ago
monetm questions
Fri, Feb 26, 21, 00:00, 3 Years ago
Wed, Sep 9, 20, 00:00, 4 Years ago
Sun, Jul 26, 20, 00:00, 4 Years ago
Thu, Jun 11, 20, 00:00, 4 Years ago
;