diff --git a/README.md b/README.md index e255cab..3d375e8 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ $('.jscroll').jscroll({ * debug (false) When set to true, outputs useful information to the console display if the `console` object exists. * autoTrigger (true) When set to true, triggers the loading of the next set of content automatically when the user scrolls to the bottom of the containing element. When set to false, the required next link will trigger the loading of the next set of content when clicked. -* autoTriggerUntil (false) Set to an integer great than 0 to turn off `autoTrigger` of paging after the specified number of pages. Requires `autoTrigger` to be `true`. +* autoTriggerUntil (false) Set to an integer great than 0 to turn off `autoTrigger` of paging after the specified number of pages. You can also provide a function that will be called after each page load (return false to stop loading). Requires `autoTrigger` to be `true`. * loadingHtml (`'Loading...'`) The HTML to show at the bottom of the content while loading the next set. * padding (0) The distance from the bottom of the scrollable content at which to trigger the loading of the next set of content. This only applies when autoTrigger is set to true. * nextSelector ('a:last') The selector to use for finding the link which contains the href pointing to the next set of content. If this selector is not found, or if it does not contain a href attribute, jScroll will self-destroy and unbind from the element upon which it was called. diff --git a/jquery.jscroll.js b/jquery.jscroll.js index 829bdb7..8fda1f8 100644 --- a/jquery.jscroll.js +++ b/jquery.jscroll.js @@ -12,7 +12,7 @@ * @author Philip Klauzinski * @requires jQuery v1.4.3+ */ -(function($) { +(function ($) { // Define the jscroll namespace and default settings $.jscroll = { @@ -30,7 +30,7 @@ }; // Constructor - var jScroll = function($e, options) { + var jScroll = function ($e, options) { // Private vars var _data = $e.data('jscroll'), @@ -44,7 +44,7 @@ _nextHref = $.trim(_$next.attr('href') + ' ' + _options.contentSelector); // Initialization - $e.data('jscroll', $.extend({}, _data, {initialized: true, waiting: false, nextHref: _nextHref})); + $e.data('jscroll', $.extend({}, _data, { initialized: true, waiting: false, nextHref: _nextHref })); _wrapInnerContent(); _preloadImage(); _setBindings(); @@ -118,23 +118,23 @@ return true; } } - + function _setBindings() { var $next = $e.find(_options.nextSelector).first(); - if (_options.autoTrigger && (_options.autoTriggerUntil === false || _options.autoTriggerUntil > 0)) { + if (_options.autoTrigger && ((_options.autoTriggerUntil === false || _options.autoTriggerUntil > 0) || ($.isFunction(_options.autoTriggerUntil) && _options.autoTriggerUntil()))) { _nextWrap($next); if (_$body.height() <= _$window.height()) { _observe(); } - _$scroll.unbind('.jscroll').bind('scroll.jscroll', function() { + _$scroll.unbind('.jscroll').bind('scroll.jscroll', function () { return _observe(); }); - if (_options.autoTriggerUntil > 0) { + if (_options.autoTriggerUntil > 0 && !$.isFunction(_options.autoTriggerUntil)) { _options.autoTriggerUntil--; } } else { _$scroll.unbind('.jscroll'); - $next.bind('click.jscroll', function() { + $next.bind('click.jscroll', function () { _nextWrap($next); _load(); return false; @@ -152,8 +152,8 @@ .children('.jscroll-added').last() .html('
' + _options.loadingHtml + '
'); - return $e.animate({scrollTop: $inner.outerHeight()}, 0, function() { - $inner.find('div.jscroll-added').last().load(data.nextHref, function(r, status, xhr) { + return $e.animate({ scrollTop: $inner.outerHeight() }, 0, function () { + $inner.find('div.jscroll-added').last().load(data.nextHref, function (r, status, xhr) { if (status === 'error') { return _destroy(); } @@ -197,8 +197,8 @@ }; // Define the jscroll plugin method and loop - $.fn.jscroll = function(m) { - return this.each(function() { + $.fn.jscroll = function (m) { + return this.each(function () { var $this = $(this), data = $this.data('jscroll'); // Instantiate jScroll on this element if it hasn't been already diff --git a/jquery.jscroll.min.js b/jquery.jscroll.min.js index f39469a..f972a91 100644 --- a/jquery.jscroll.min.js +++ b/jquery.jscroll.min.js @@ -12,4 +12,4 @@ * @author Philip Klauzinski * @requires jQuery v1.4.3+ */ -(function(b){b.jscroll={defaults:{debug:false,autoTrigger:true,autoTriggerUntil:false,loadingHtml:"Loading...",padding:0,nextSelector:"a:last",contentSelector:"",pagingSelector:"",callback:false}};var a=function(e,g){var o=e.data("jscroll"),n=(typeof g==="function")?{callback:g}:g,p=b.extend({},b.jscroll.defaults,n,o||{}),c=(e.css("overflow-y")==="visible"),l=e.find(p.nextSelector).first(),v=b(window),h=b("body"),q=c?v:e,m=b.trim(l.attr("href")+" "+p.contentSelector);e.data("jscroll",b.extend({},o,{initialized:true,waiting:false,nextHref:m}));r();k();t();function k(){var x=b(p.loadingHtml).filter("img").attr("src");if(x){var w=new Image();w.src=x}}function r(){if(!e.find(".jscroll-inner").length){e.contents().wrapAll('
')}}function d(w){if(p.pagingSelector){var x=w.closest(p.pagingSelector).hide()}else{var x=w.parent().not(".jscroll-inner,.jscroll-added").addClass("jscroll-next-parent").hide();if(!x.length){w.wrap('
').parent().hide()}}}function j(){return q.unbind(".jscroll").removeData("jscroll").find(".jscroll-inner").children().unwrap().filter(".jscroll-added").children().unwrap()}function i(){r();var D=e.find("div.jscroll-inner").first(),B=e.data("jscroll"),C=parseInt(e.css("borderTopWidth")),y=isNaN(C)?0:C,x=parseInt(e.css("paddingTop"))+y,A=c?q.scrollTop():e.offset().top,z=D.length?D.offset().top:0,w=Math.ceil(A-z+q.height()+x);if(!B.waiting&&w+p.padding>=D.outerHeight()){f("info","jScroll:",D.outerHeight()-w,"from bottom. Loading next request...");return u()}}function s(w){w=w||e.data("jscroll");if(!w||!w.nextHref){f("warn","jScroll: nextSelector not found - destroying");j();return false}else{t();return true}}function t(){var w=e.find(p.nextSelector).first();if(p.autoTrigger&&(p.autoTriggerUntil===false||p.autoTriggerUntil>0)){d(w);if(h.height()<=v.height()){i()}q.unbind(".jscroll").bind("scroll.jscroll",function(){return i()});if(p.autoTriggerUntil>0){p.autoTriggerUntil--}}else{q.unbind(".jscroll");w.bind("click.jscroll",function(){d(w);u();return false})}}function u(){var x=e.find("div.jscroll-inner").first(),w=e.data("jscroll");w.waiting=true;x.append('
').children(".jscroll-added").last().html('
'+p.loadingHtml+"
");return e.animate({scrollTop:x.outerHeight()},0,function(){x.find("div.jscroll-added").last().load(w.nextHref,function(A,z,B){if(z==="error"){return j()}var y=b(this).find(p.nextSelector).first();w.waiting=false;w.nextHref=y.attr("href")?b.trim(y.attr("href")+" "+p.contentSelector):false;b(".jscroll-next-parent",e).remove();s();if(p.callback){p.callback.call(this)}f("dir",w)})})}function f(w){if(p.debug&&typeof console==="object"&&(typeof w==="object"||typeof console[w]==="function")){if(typeof w==="object"){var y=[];for(var x in w){if(typeof console[x]==="function"){y=(w[x].length)?w[x]:[w[x]];console[x].apply(console,y)}else{console.log.apply(console,y)}}}else{console[w].apply(console,Array.prototype.slice.call(arguments,1))}}}b.extend(e.jscroll,{destroy:j});return e};b.fn.jscroll=function(c){return this.each(function(){var f=b(this),e=f.data("jscroll");if(e&&e.initialized){return}var d=new a(f,c)})}})(jQuery); \ No newline at end of file +(function(e){e.jscroll={defaults:{debug:false,autoTrigger:true,autoTriggerUntil:false,loadingHtml:"Loading...",padding:0,nextSelector:"a:last",contentSelector:"",pagingSelector:"",callback:false}};var t=function(t,n){function h(){var t=e(s.loadingHtml).filter("img").attr("src");if(t){var n=new Image;n.src=t}}function p(){if(!t.find(".jscroll-inner").length){t.contents().wrapAll('
')}}function d(e){if(s.pagingSelector){var t=e.closest(s.pagingSelector).hide()}else{var t=e.parent().not(".jscroll-inner,.jscroll-added").addClass("jscroll-next-parent").hide();if(!t.length){e.wrap('
').parent().hide()}}}function v(){return l.unbind(".jscroll").removeData("jscroll").find(".jscroll-inner").children().unwrap().filter(".jscroll-added").children().unwrap()}function m(){p();var e=t.find("div.jscroll-inner").first(),n=t.data("jscroll"),r=parseInt(t.css("borderTopWidth")),i=isNaN(r)?0:r,u=parseInt(t.css("paddingTop"))+i,a=o?l.scrollTop():t.offset().top,f=e.length?e.offset().top:0,c=Math.ceil(a-f+l.height()+u);if(!n.waiting&&c+s.padding>=e.outerHeight()){w("info","jScroll:",e.outerHeight()-c,"from bottom. Loading next request...");return b()}}function g(e){e=e||t.data("jscroll");if(!e||!e.nextHref){w("warn","jScroll: nextSelector not found - destroying");v();return false}else{y();return true}}function y(){var n=t.find(s.nextSelector).first();if(s.autoTrigger&&(s.autoTriggerUntil===false||s.autoTriggerUntil>0||e.isFunction(s.autoTriggerUntil)&&s.autoTriggerUntil())){d(n);if(f.height()<=a.height()){m()}l.unbind(".jscroll").bind("scroll.jscroll",function(){return m()});if(s.autoTriggerUntil>0&&!e.isFunction(s.autoTriggerUntil)){s.autoTriggerUntil--}}else{l.unbind(".jscroll");n.bind("click.jscroll",function(){d(n);b();return false})}}function b(){var n=t.find("div.jscroll-inner").first(),r=t.data("jscroll");r.waiting=true;n.append('
').children(".jscroll-added").last().html('
'+s.loadingHtml+"
");return t.animate({scrollTop:n.outerHeight()},0,function(){n.find("div.jscroll-added").last().load(r.nextHref,function(n,i,o){if(i==="error"){return v()}var u=e(this).find(s.nextSelector).first();r.waiting=false;r.nextHref=u.attr("href")?e.trim(u.attr("href")+" "+s.contentSelector):false;e(".jscroll-next-parent",t).remove();g();if(s.callback){s.callback.call(this)}w("dir",r)})})}function w(e){if(s.debug&&typeof console==="object"&&(typeof e==="object"||typeof console[e]==="function")){if(typeof e==="object"){var t=[];for(var n in e){if(typeof console[n]==="function"){t=e[n].length?e[n]:[e[n]];console[n].apply(console,t)}else{console.log.apply(console,t)}}}else{console[e].apply(console,Array.prototype.slice.call(arguments,1))}}}var r=t.data("jscroll"),i=typeof n==="function"?{callback:n}:n,s=e.extend({},e.jscroll.defaults,i,r||{}),o=t.css("overflow-y")==="visible",u=t.find(s.nextSelector).first(),a=e(window),f=e("body"),l=o?a:t,c=e.trim(u.attr("href")+" "+s.contentSelector);t.data("jscroll",e.extend({},r,{initialized:true,waiting:false,nextHref:c}));p();h();y();e.extend(t.jscroll,{destroy:v});return t};e.fn.jscroll=function(n){return this.each(function(){var r=e(this),i=r.data("jscroll");if(i&&i.initialized)return;var s=new t(r,n)})}})(jQuery) \ No newline at end of file