diff --git a/jquery.jscroll.js b/jquery.jscroll.js index 06fb462..ea578ae 100644 --- a/jquery.jscroll.js +++ b/jquery.jscroll.js @@ -31,7 +31,8 @@ contentSelector: '', pagingSelector: '', callback: false - } + }, + instances: 0 }; // Constructor @@ -79,8 +80,10 @@ // Remove the jscroll behavior and data from an element _destroy = function() { - return _$scroll.unbind('.jscroll') - .removeData('jscroll') + var data = $e.data('jscroll'); + $e.removeData('jscroll'); + return _$scroll.unbind('.jscroll.instance_'+data.id) + //.removeData('jscroll') //CI: think this is obsolete .find('.jscroll-inner').children().unwrap() .filter('.jscroll-added').children().unwrap(); }, @@ -100,7 +103,7 @@ if (!data.waiting && iTotalHeight + _options.padding >= $inner.outerHeight()) { //data.nextHref = $.trim(data.nextHref + ' ' + _options.contentSelector); - _debug('info', 'jScroll:', $inner.outerHeight() - iTotalHeight, 'from bottom. Loading next request...'); + _debug('info', 'jScroll[' + data.id + ']:', $inner.outerHeight() - iTotalHeight, 'from bottom. Loading next request...'); return _load(); } } @@ -110,7 +113,7 @@ _checkNextHref = function(data) { data = data || $e.data('jscroll'); if (!data || !data.nextHref) { - _debug('warn', 'jScroll: nextSelector not found - destroying'); + _debug('warn', 'jScroll[' + data.id + ']: nextSelector not found - destroying'); _destroy(); return false; } else { @@ -120,27 +123,28 @@ }, _setBindings = function() { - var $next = $e.find(_options.nextSelector).first(); + var $next = $e.find(_options.nextSelector).first(), + data = $e.data('jscroll'); if (!$next.length) { return; } if (_options.autoTrigger && (_options.autoTriggerUntil === false || _options.autoTriggerUntil > 0)) { _nextWrap($next); - var scrollingBodyHeight = _$body.height() - $e.offset().top, - scrollingHeight = ($e.height() < scrollingBodyHeight) ? $e.height() : scrollingBodyHeight, - windowHeight = ($e.offset().top - _$window.scrollTop() > 0) ? _$window.height() - ($e.offset().top - $(window).scrollTop()) : _$window.height(); + var scrollingBodyHeight = _$body.height() - $e.offset().top, + scrollingHeight = ($e.height() < scrollingBodyHeight) ? $e.height() : scrollingBodyHeight, + windowHeight = ($e.offset().top - _$window.scrollTop() > 0) ? _$window.height() - ($e.offset().top - $(window).scrollTop()) : _$window.height(); if (scrollingHeight <= windowHeight) { _observe(); } - _$scroll.unbind('.jscroll').bind('scroll.jscroll', function() { + _$scroll.unbind('.jscroll.instance_'+data.id).bind('scroll.jscroll.instance_'+data.id, function() { return _observe(); }); if (_options.autoTriggerUntil > 0) { _options.autoTriggerUntil--; } } else { - _$scroll.unbind('.jscroll'); - $next.bind('click.jscroll', function() { + _$scroll.unbind('.jscroll.instance_'+data.id); + $next.bind('click.jscroll.instance_'+data.id, function() { _nextWrap($next); _load(); return false; @@ -156,7 +160,7 @@ data.waiting = true; $inner.append('
') .children('.jscroll-added').last() - .html('
' + _options.loadingHtml + '
') + .html('
' + _options.loadingHtml + '
') .promise() .done(function(){ if (_options.loadingFunction) { @@ -202,7 +206,7 @@ }; // Initialization - $e.data('jscroll', $.extend({}, _data, {initialized: true, waiting: false, nextHref: _nextHref})); + $e.data('jscroll', $.extend({}, _data, {initialized: true, waiting: false, nextHref: _nextHref, id: ++$.jscroll.instances})); _wrapInnerContent(); _preloadImage(); _setBindings(); diff --git a/jquery.jscroll.min.js b/jquery.jscroll.min.js index 4f099d1..97e523e 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...",loadingFunction:!1,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(){if(b.is(":visible")){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);if(!c.waiting&&l+f.padding>=a.outerHeight())return u("info","jScroll:",a.outerHeight()-l,"from bottom. Loading next request..."),t()}},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 c=b.find(f.nextSelector).first();if(c.length)if(f.autoTrigger&&(f.autoTriggerUntil===!1||f.autoTriggerUntil>0)){o(c);var d=j.height()-b.offset().top,e=b.height()0?i.height()-(b.offset().top-a(window).scrollTop()):i.height();g>=e&&q(),k.unbind(".jscroll").bind("scroll.jscroll",function(){return q()}),f.autoTriggerUntil>0&&f.autoTriggerUntil--}else k.unbind(".jscroll"),c.bind("click.jscroll",function(){return o(c),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+"
").promise().done(function(){f.loadingFunction&&f.loadingFunction()}),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(a){"use strict";a.jscroll={defaults:{debug:!1,autoTrigger:!0,autoTriggerUntil:!1,loadingHtml:"Loading...",loadingFunction:!1,padding:0,nextSelector:"a:last",contentSelector:"",pagingSelector:"",callback:!1},instances:0};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(){var a=b.data("jscroll");return b.removeData("jscroll"),k.unbind(".jscroll.instance_"+a.id).find(".jscroll-inner").children().unwrap().filter(".jscroll-added").children().unwrap()},q=function(){if(b.is(":visible")){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);if(!c.waiting&&l+f.padding>=a.outerHeight())return u("info","jScroll["+c.id+"]:",a.outerHeight()-l,"from bottom. Loading next request..."),t()}},r=function(a){return a=a||b.data("jscroll"),a&&a.nextHref?(s(),!0):(u("warn","jScroll["+a.id+"]: nextSelector not found - destroying"),p(),!1)},s=function(){var c=b.find(f.nextSelector).first(),d=b.data("jscroll");if(c.length)if(f.autoTrigger&&(f.autoTriggerUntil===!1||f.autoTriggerUntil>0)){o(c);var e=j.height()-b.offset().top,g=b.height()0?i.height()-(b.offset().top-a(window).scrollTop()):i.height();g<=h&&q(),k.unbind(".jscroll.instance_"+d.id).bind("scroll.jscroll.instance_"+d.id,function(){return q()}),f.autoTriggerUntil>0&&f.autoTriggerUntil--}else k.unbind(".jscroll.instance_"+d.id),c.bind("click.jscroll.instance_"+d.id,function(){return o(c),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+"
").promise().done(function(){f.loadingFunction&&f.loadingFunction()}),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),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,id:++a.jscroll.instances})),n(),m(),s(),a.extend(b.jscroll,{destroy:p}),b};a.fn.jscroll=function(c){return this.each(function(){var f,d=a(this),e=d.data("jscroll");e&&e.initialized||(f=new b(d,c))})}}(jQuery);