Created
February 5, 2009 20:20
-
-
Save brianmario/58975 to your computer and use it in GitHub Desktop.
jQuery infinite scroll plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Infinite Scroll | |
jQuery.fn.infiniteScroll = function(options) { | |
return $(this).each(function() { | |
var el = $(this); | |
var settings = jQuery.extend({ | |
url: null, | |
triggerAt: 300, | |
page: 2, | |
appendTo: '.list', | |
container: $(document) | |
}, options); | |
var req = null; | |
var maxReached = false; | |
var infinityRunner = function() { | |
if (settings.url !== null) { | |
if (settings.triggerAt >= (settings.container.height() - el.height() - el.scrollTop())) { | |
// if the request is in progress, exit and wait for it to finish | |
if (req && req.readyState < 4 && req.readyState > 0) { | |
return; | |
} | |
req = $.get(settings.url, 'page='+settings.page, function(data) { | |
if (data !== '') { | |
$(settings.appendTo).append(data); | |
settings.page++; | |
} else { | |
maxReached = true; | |
} | |
}, 'html'); | |
} | |
} | |
}; | |
el.scroll(function(e) { | |
if (!maxReached) { | |
infinityRunner(); | |
} | |
}); | |
// Test initial page layout for trigger | |
infinityRunner(); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment