Skip to content

Instantly share code, notes, and snippets.

@elclanrs
Last active April 17, 2016 00:39
Show Gist options
  • Save elclanrs/5514942 to your computer and use it in GitHub Desktop.
Save elclanrs/5514942 to your computer and use it in GitHub Desktop.
Simple Infinite Scroll
/**
* jQuery throttle / debounce - v1.1 - 3/7/2010
* http://benalman.com/projects/jquery-throttle-debounce-plugin/
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
(function(b,c){var $=b.jQuery||b.Cowboy||(b.Cowboy={}),a;$.throttle=a=function(e,f,j,i){var h,d=0;if(typeof f!=="boolean"){i=j;j=f;f=c}function g(){var o=this,m=+new Date()-d,n=arguments;function l(){d=+new Date();j.apply(o,n)}function k(){h=c}if(i&&!h){l()}h&&clearTimeout(h);if(i===c&&m>e){l()}else{if(f!==true){h=setTimeout(i?k:l,i===c?e-m:e)}}}if($.guid){g.guid=j.guid=j.guid||$.guid++}return g};$.debounce=function(d,e,f){return f===c?a(d,e,false):a(d,f,e!==false)}})(this);
/**
* keepScrolling: Simple infinite scroll
* @author Cedric Ruiz
* @requires http://benalman.com/projects/jquery-throttle-debounce-plugin/
* @usage
* $('#content').keepScrolling({ options });
*/
(function($, win, doc) {
$.fn.keepScrolling = function(opts) {
var _defaults = {
item: null, // item selector to be appended
pagination: null, // pagination container
next: null, // next page link
offset: 0, // scroll offset
hidden: false, // add items hidden?
loader: 'Loading...', // loading html
delay: 0, // delay the ajax request
before: null, // callback before loading content
after: null // callback after loading content
};
return this.each(function() {
var o = $.extend({}, _defaults, opts),
$loader = $('<div class="infinite-loading">'+ o.loader +'</div>'),
busy = false,
self = this;
function load(data) {
var next = $(o.next).attr('href'),
$items = $(data).find(o.item),
$newPagination = $(data).find(o.pagination);
$(o.pagination).empty();
$loader.remove();
if ($newPagination.length) {
$(o.pagination).append($newPagination.children());
}
if (o.hidden) $items.hide();
$(self).append($items);
if (o.after) o.after($items, next);
busy = false;
}
$(win).scroll($.throttle(250, function() {
var next = $(o.next).attr('href'),
endOfPage = $(doc).height() - $(win).height();
// Load next page
if (!busy && next) {
if (o.before) o.before(next);
// Load content
if ($(win).scrollTop() >= endOfPage - o.offset) {
busy = true;
$(self).append($loader);
setTimeout(function(){ $.get(next, load); }, o.delay);
}
}
}));
$(o.pagination).hide(); // start fresh
});
};
}(jQuery, window, document));
(function(a,b){var d,c=a.jQuery||a.Cowboy||(a.Cowboy={});c.throttle=d=function(a,d,e,f){function i(){function k(){h=+new Date,e.apply(c,j)}function l(){g=b}var c=this,i=+new Date-h,j=arguments;f&&!g&&k(),g&&clearTimeout(g),f===b&&i>a?k():d!==!0&&(g=setTimeout(f?l:k,f===b?a-i:a))}var g,h=0;return"boolean"!=typeof d&&(f=e,e=d,d=b),c.guid&&(i.guid=e.guid=e.guid||c.guid++),i},c.debounce=function(a,c,e){return e===b?d(a,c,!1):d(a,e,c!==!1)}})(this),function(a,b,c){a.fn.keepScrolling=function(d){var e={item:null,pagination:null,next:null,offset:0,hidden:!1,loader:"Loading...",delay:0,before:null,after:null};return this.each(function(){function j(b){var c=a(f.next).attr("href"),d=a(b).find(f.item),e=a(b).find(f.pagination);a(f.pagination).empty(),g.remove(),e.length&&a(f.pagination).append(e.children()),f.hidden&&d.hide(),a(i).append(d),f.after&&f.after(d,c),h=!1}var f=a.extend({},e,d),g=a('<div class="infinite-loading">'+f.loader+"</div>"),h=!1,i=this;a(b).scroll(a.throttle(250,function(){var d=a(f.next).attr("href"),e=a(c).height()-a(b).height();!h&&d&&(f.before&&f.before(d),a(b).scrollTop()>=e-f.offset&&(h=!0,a(i).append(g),setTimeout(function(){a.get(d,j)},f.delay)))})),a(f.pagination).hide()})}}(jQuery,window,document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment