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('