diff --git a/jquery.jscroll.js b/jquery.jscroll.js index 2546f7c..90284bd 100644 --- a/jquery.jscroll.js +++ b/jquery.jscroll.js @@ -45,7 +45,8 @@ _$window = $(window), _$body = $('body'), _$scroll = _isWindow ? _$window : $e, - _nextHref = $.trim(_$next.attr('href') + ' ' + _options.contentSelector), + _nextHref = _$next.attr('href') ? $.trim(_$next.attr('href') + ' ' + _options.contentSelector) : false, + _unique_id = Math.floor((Math.random() * 10000) + 1), // Check if a loading image is defined and preload _preloadImage = function() { @@ -78,10 +79,11 @@ // Remove the jscroll behavior and data from an element _destroy = function() { - return _$scroll.unbind('.jscroll') - .removeData('jscroll') - .find('.jscroll-inner').children().unwrap() - .filter('.jscroll-added').children().unwrap(); + _$scroll.unbind('.jscroll' + _unique_id); + $e.removeData('jscroll'); + $e.find('.jscroll-inner').children().unwrap(); + $e.filter('.jscroll-added').children().unwrap(); + $e.find('.jscroll-loading').remove(); }, // Observe the scroll event for when to trigger the next load @@ -96,7 +98,7 @@ innerTop = $inner.length ? $inner.offset().top : 0, iTotalHeight = Math.ceil(iTopHeight - innerTop + _$scroll.height() + iContainerTop); - if (!data.waiting && iTotalHeight + _options.padding >= $inner.outerHeight()) { + if (!data.waiting && $inner.outerHeight() > 0 && iTotalHeight + _options.padding >= $inner.outerHeight()) { //data.nextHref = $.trim(data.nextHref + ' ' + _options.contentSelector); _debug('info', 'jScroll:', $inner.outerHeight() - iTotalHeight, 'from bottom. Loading next request...'); return _load(); @@ -123,15 +125,15 @@ if (_$body.height() <= _$window.height()) { _observe(); } - _$scroll.unbind('.jscroll').bind('scroll.jscroll', function() { + _$scroll.unbind('.jscroll' + _unique_id).bind('scroll.jscroll' + _unique_id, function() { return _observe(); }); if (_options.autoTriggerUntil > 0) { _options.autoTriggerUntil--; } } else { - _$scroll.unbind('.jscroll'); - $next.bind('click.jscroll', function() { + _$scroll.unbind('.jscroll' + _unique_id); + $next.bind('click.jscroll' + _unique_id, function() { _nextWrap($next); _load(); return false; @@ -187,10 +189,11 @@ }; // Initialization - $e.data('jscroll', $.extend({}, _data, {initialized: true, waiting: false, nextHref: _nextHref})); + $e.data('jscroll', $.extend({}, _data, {initialized: true, waiting: false, nextHref: _nextHref, unique_id: _unique_id})); _wrapInnerContent(); _preloadImage(); _setBindings(); + _checkNextHref(); // Expose API methods via the jQuery.jscroll namespace, e.g. $('sel').jscroll.method() $.extend($e.jscroll, { diff --git a/jquery.jscroll.min.js b/jquery.jscroll.min.js index 4df1fb1..1f6f79d 100644 --- a/jquery.jscroll.min.js +++ b/jquery.jscroll.min.js @@ -14,4 +14,4 @@ * @requires jQuery v1.4.3+ * @preserve */ -!function(a){"use strict";a.jscroll={defaults:{debug:!1,autoTrigger:!0,autoTriggerUntil:!1,loadingHtml:"Loading...",padding:0,nextSelector:"a:last",contentSelector:"",pagingSelector:"",callback:!1}};var b=function(b,c){var d=b.data("jscroll"),e="function"==typeof c?{callback:c}:c,f=a.extend({},a.jscroll.defaults,e,d||{}),g="visible"===b.css("overflow-y"),h=b.find(f.nextSelector).first(),i=a(window),j=a("body"),k=g?i:b,l=a.trim(h.attr("href")+" "+f.contentSelector),m=function(){var b=a(f.loadingHtml).filter("img").attr("src");if(b){var c=new Image;c.src=b}},n=function(){b.find(".jscroll-inner").length||b.contents().wrapAll('
')},o=function(a){var b;f.pagingSelector?a.closest(f.pagingSelector).hide():(b=a.parent().not(".jscroll-inner,.jscroll-added").addClass("jscroll-next-parent").hide(),b.length||a.wrap('
').parent().hide())},p=function(){return k.unbind(".jscroll").removeData("jscroll").find(".jscroll-inner").children().unwrap().filter(".jscroll-added").children().unwrap()},q=function(){n();var a=b.find("div.jscroll-inner").first(),c=b.data("jscroll"),d=parseInt(b.css("borderTopWidth"),10),e=isNaN(d)?0:d,h=parseInt(b.css("paddingTop"),10)+e,i=g?k.scrollTop():b.offset().top,j=a.length?a.offset().top:0,l=Math.ceil(i-j+k.height()+h);return!c.waiting&&l+f.padding>=a.outerHeight()?(u("info","jScroll:",a.outerHeight()-l,"from bottom. Loading next request..."),t()):void 0},r=function(a){return a=a||b.data("jscroll"),a&&a.nextHref?(s(),!0):(u("warn","jScroll: nextSelector not found - destroying"),p(),!1)},s=function(){var a=b.find(f.nextSelector).first();f.autoTrigger&&(f.autoTriggerUntil===!1||f.autoTriggerUntil>0)?(o(a),j.height()<=i.height()&&q(),k.unbind(".jscroll").bind("scroll.jscroll",function(){return q()}),f.autoTriggerUntil>0&&f.autoTriggerUntil--):(k.unbind(".jscroll"),a.bind("click.jscroll",function(){return o(a),t(),!1}))},t=function(){var c=b.find("div.jscroll-inner").first(),d=b.data("jscroll");return d.waiting=!0,c.append('
').children(".jscroll-added").last().html('
'+f.loadingHtml+"
"),b.animate({scrollTop:c.outerHeight()},0,function(){c.find("div.jscroll-added").last().load(d.nextHref,function(c,e){if("error"===e)return p();var g=a(this).find(f.nextSelector).first();d.waiting=!1,d.nextHref=g.attr("href")?a.trim(g.attr("href")+" "+f.contentSelector):!1,a(".jscroll-next-parent",b).remove(),r(),f.callback&&f.callback.call(this),u("dir",d)})})},u=function(a){if(f.debug&&"object"==typeof console&&("object"==typeof a||"function"==typeof console[a]))if("object"==typeof a){var b=[];for(var c in a)"function"==typeof console[c]?(b=a[c].length?a[c]:[a[c]],console[c].apply(console,b)):console.log.apply(console,b)}else console[a].apply(console,Array.prototype.slice.call(arguments,1))};return b.data("jscroll",a.extend({},d,{initialized:!0,waiting:!1,nextHref:l})),n(),m(),s(),a.extend(b.jscroll,{destroy:p}),b};a.fn.jscroll=function(c){return this.each(function(){var d,e=a(this),f=e.data("jscroll");f&&f.initialized||(d=new b(e,c))})}}(jQuery); \ No newline at end of file +!function(t){"use strict";t.jscroll={defaults:{debug:!1,autoTrigger:!0,autoTriggerUntil:!1,loadingHtml:"Loading...",padding:0,nextSelector:"a:last",contentSelector:"",pagingSelector:"",callback:!1}};var e=function(e,n){var r=e.data("jscroll"),l="function"==typeof n?{callback:n}:n,o=t.extend({},t.jscroll.defaults,l,r||{}),i="visible"===e.css("overflow-y"),a=e.find(o.nextSelector).first(),c=t(window),s=t("body"),d=i?c:e,f=a.attr("href")?t.trim(a.attr("href")+" "+o.contentSelector):!1,u=Math.floor(1e4*Math.random()+1),g=function(){var e=t(o.loadingHtml).filter("img").attr("src");if(e){var n=new Image;n.src=e}},p=function(){e.find(".jscroll-inner").length||e.contents().wrapAll('
')},j=function(t){var e;o.pagingSelector?t.closest(o.pagingSelector).hide():(e=t.parent().not(".jscroll-inner,.jscroll-added").addClass("jscroll-next-parent").hide(),e.length||t.wrap('
').parent().hide())},h=function(){d.unbind(".jscroll"+u),e.removeData("jscroll"),e.find(".jscroll-inner").children().unwrap(),e.filter(".jscroll-added").children().unwrap(),e.find(".jscroll-loading").remove()},v=function(){p();var t=e.find("div.jscroll-inner").first(),n=e.data("jscroll"),r=parseInt(e.css("borderTopWidth"),10),l=isNaN(r)?0:r,a=parseInt(e.css("paddingTop"),10)+l,c=i?d.scrollTop():e.offset().top,s=t.length?t.offset().top:0,f=Math.ceil(c-s+d.height()+a);return!n.waiting&&t.outerHeight()>0&&f+o.padding>=t.outerHeight()?(y("info","jScroll:",t.outerHeight()-f,"from bottom. Loading next request..."),x()):void 0},b=function(t){return t=t||e.data("jscroll"),t&&t.nextHref?(m(),!0):(y("warn","jScroll: nextSelector not found - destroying"),h(),!1)},m=function(){var t=e.find(o.nextSelector).first();o.autoTrigger&&(o.autoTriggerUntil===!1||o.autoTriggerUntil>0)?(j(t),s.height()<=c.height()&&v(),d.unbind(".jscroll"+u).bind("scroll.jscroll"+u,function(){return v()}),o.autoTriggerUntil>0&&o.autoTriggerUntil--):(d.unbind(".jscroll"+u),t.bind("click.jscroll"+u,function(){return j(t),x(),!1}))},x=function(){var n=e.find("div.jscroll-inner").first(),r=e.data("jscroll");return r.waiting=!0,n.append('
').children(".jscroll-added").last().html('
'+o.loadingHtml+"
"),e.animate({scrollTop:n.outerHeight()},0,function(){n.find("div.jscroll-added").last().load(r.nextHref,function(n,l){if("error"===l)return h();var i=t(this).find(o.nextSelector).first();r.waiting=!1,r.nextHref=i.attr("href")?t.trim(i.attr("href")+" "+o.contentSelector):!1,t(".jscroll-next-parent",e).remove(),b(),o.callback&&o.callback.call(this),y("dir",r)})})},y=function(t){if(o.debug&&"object"==typeof console&&("object"==typeof t||"function"==typeof console[t]))if("object"==typeof t){var e=[];for(var n in t)"function"==typeof console[n]?(e=t[n].length?t[n]:[t[n]],console[n].apply(console,e)):console.log.apply(console,e)}else console[t].apply(console,Array.prototype.slice.call(arguments,1))};return e.data("jscroll",t.extend({},r,{initialized:!0,waiting:!1,nextHref:f,unique_id:u})),p(),g(),m(),b(),t.extend(e.jscroll,{destroy:h}),e};t.fn.jscroll=function(n){return this.each(function(){var r,l=t(this),o=l.data("jscroll");o&&o.initialized||(r=new e(l,n))})}}(jQuery);