Alan Edwardes

Endless Page Scroll With jQuery

jQuery is a fantastic platform. It allows you to manipulate data quickly and easily, and on the most part you don't have to care about browser compatibility in the slightest.

On my new homepage I wrote a quick little snippet of jQuery to make the browser request more posts when the user gets to the bottom of the page, which, providing every user that goes onto my site doesn't hold down page down constantly is a elegant way to deliver more content to the user without bombarding them with links to other pages.

The Code

The code is simple; get the height of the page, the amount the user has scrolled down (from the top of the page) add those figures together and see if they equal the total page height s+h == h (if 's' is the amount scrolled and h the height of the page). The result:
$(window).scroll(function(){
	if($(document).scrollTop()+$(window).height() == $(document).height()){
		loadmorefunction();
	}
});

That code triggers when the user scrolls; and if the user is at the bottom of the page it triggers a function.

To do the same for a scrollable element you can use exactly the same method if you replace "window" with the element you want to use.

1 Comment

Matthew's Gravatar
1

Without any pageless and infinte scrolls, it's that simple, dude, you saved my life, thanks! ;)

Add a Comment

01st of September 2009 at 6:44 AM

2 years, 8 months ago

written by Alan Edwardes.

204 words

1 comment so far

feed for comments on this post

rand: Windows 7 + Windows Media ...

next: Alan Edwardes 3.0

prev: Slicehost

share:FacebookTwitterRedditdiggStumbleUpondeliciousHacker NewsLinkedIn

add a comment

© 2006 – 2012 Alan Edwardes / code on github
Top