diff --git a/README.md b/README.md index e255cab..e0b9793 100644 --- a/README.md +++ b/README.md @@ -36,15 +36,16 @@ $('.jscroll').jscroll({ ## Options -* 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`. -* 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. -* contentSelector ('') A convenience selector for loading only part of the content in the response for the next set of content. This selector will be ignored if left blank and will apply the entire response to the DOM. -* pagingSelector ('') Optionally define a selector for your paging controls so that they will be hidden, instead of just hiding the next page link. -* callback () Optionally define a callback function to be called after a set of content has been loaded. +* 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`. +* 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. +* nextHrefAttribute ('href') The html attribute where the next page url is contained. This allows to use html5 data attributes to to store the next page url. +* contentSelector ('') A convenience selector for loading only part of the content in the response for the next set of content. This selector will be ignored if left blank and will apply the entire response to the DOM. +* pagingSelector ('') Optionally define a selector for your paging controls so that they will be hidden, instead of just hiding the next page link. +* callback () Optionally define a callback function to be called after a set of content has been loaded. For more information on the *contentSelector* option and how it loads a response fragment, see the [jQuery documentation for the .load() method](http://api.jquery.com/load/). diff --git a/jquery.jscroll.js b/jquery.jscroll.js index 829bdb7..12b3660 100644 --- a/jquery.jscroll.js +++ b/jquery.jscroll.js @@ -23,6 +23,7 @@ loadingHtml: 'Loading...', padding: 0, nextSelector: 'a:last', + nextHrefAttribute: 'href', contentSelector: '', pagingSelector: '', callback: false @@ -41,7 +42,7 @@ _$window = $(window), _$body = $('body'), _$scroll = _isWindow ? _$window : $e, - _nextHref = $.trim(_$next.attr('href') + ' ' + _options.contentSelector); + _nextHref = $.trim(_$next.attr(options.nextHrefAttribute) + ' ' + _options.contentSelector); // Initialization $e.data('jscroll', $.extend({}, _data, {initialized: true, waiting: false, nextHref: _nextHref})); @@ -159,7 +160,7 @@ } var $next = $(this).find(_options.nextSelector).first(); data.waiting = false; - data.nextHref = $next.attr('href') ? $.trim($next.attr('href') + ' ' + _options.contentSelector) : false; + data.nextHref = $next.attr(_options.nextHrefAttribute) ? $.trim($next.attr(_options.nextHrefAttribute) + ' ' + _options.contentSelector) : false; $('.jscroll-next-parent', $e).remove(); // Remove the previous next link now that we have a new one _checkNextHref(); if (_options.callback) { diff --git a/jquery.jscroll.min.js b/jquery.jscroll.min.js index f39469a..38f8986 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",nextHrefAttribute:"href",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 e=t.find(s.nextSelector).first();if(s.autoTrigger&&(s.autoTriggerUntil===false||s.autoTriggerUntil>0)){d(e);if(f.height()<=a.height()){m()}l.unbind(".jscroll").bind("scroll.jscroll",function(){return m()});if(s.autoTriggerUntil>0){s.autoTriggerUntil--}}else{l.unbind(".jscroll");e.bind("click.jscroll",function(){d(e);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(s.nextHrefAttribute)?e.trim(u.attr(s.nextHrefAttribute)+" "+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(n.nextHrefAttribute)+" "+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