Heres the jquery script I wrote recently to get 9gag posts as I scroll. I hope it helps you:
Note the magic for the check if you have scrolled to the bottom happens in $(window).scroll
function
<script>
var nextPageId = '0'
function loadNextPage(){
$.getJSON("http://infinigag.eu01.aws.af.cm/trending/"+nextPageId, function(response){
$.each(response.data, function(){
var $img = $('<img/>', {src: this.images.large})
$('body').append($img)
})
nextPageId = response.paging.next
})
}
loadNextPage()
var debounceNextPage = debounce(loadNextPage, 100)
var $document = $(document)
var $window = $(window)
$(window).scroll(function(e){
// console.log(e)
if($document.height() - $window.scrollTop() == $window.height()){
console.log("bottom");
// loadNextPage()
debounceNextPage()
}
})
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
</script>
EDIT
$(".box").scroll(function() {
if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
$("span").show();
} else {
$("span").hide();
}
});