diff --git a/jquery.ba-resize.js b/jquery.ba-resize.js index 1f41d37..a00be2e 100644 --- a/jquery.ba-resize.js +++ b/jquery.ba-resize.js @@ -145,7 +145,11 @@ $.data( this, str_data, { w: elem.width(), h: elem.height() } ); // If this is the first element added, start the polling loop. - if ( elems.length === 1 ) { + if ( elems.length === 1 ) { + //set the timeout_id to undefined. + timeout_id = undefined; + + //start the loop loopy(); } }, @@ -168,7 +172,10 @@ // If this is the last element removed, stop the polling loop. if ( !elems.length ) { - clearTimeout( timeout_id ); + cancelAnimationFrame( timeout_id ); + + //set the timeout_id to null, to make sure the loop is stopped + timeout_id = null; } }, @@ -216,11 +223,7 @@ }; - function loopy() { - - // Start the polling loop, asynchronously. - timeout_id = window[ str_setTimeout ](function(){ - + function loopy() { // Iterate over all elements to which the 'resize' event is bound. elems.each(function(){ var elem = $(this), @@ -235,12 +238,39 @@ } }); - - // Loop. - loopy(); - - }, jq_resize[ str_delay ] ); - + + //request another animationFrame to poll the elements + if(timeout_id !== null) + timeout_id = window.requestAnimationFrame(loopy); }; -})(jQuery,this); + /** + * Provides requestAnimationFrame in a cross browser way. + * http://paulirish.com/2011/requestanimationframe-for-smart-animating/ + */ + if ( !window.requestAnimationFrame ) { + window.requestAnimationFrame = (function(){ + return window.webkitRequestAnimationFrame || + window.mozRequestAnimationFrame || + window.oRequestAnimationFrame || + window.msRequestAnimationFrame || + function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) { + return window.setTimeout( callback, jq_resize[ str_delay ] ); + }; + })(); + } + /** + * Provides cancelAnimationFrame in a cross browser way. + */ + if( !window.cancelAnimationFrame ){ + window.cancelAnimationFrame = ( function() { + return window.webkitCancelRequestAnimationFrame || + window.mozCancelRequestAnimationFrame || + window.oCancelRequestAnimationFrame || + window.msCancelRequestAnimationFrame || + clearTimeout + })(); + } + + +})(jQuery,this); \ No newline at end of file diff --git a/jquery.ba-resize.min.js b/jquery.ba-resize.min.js index c678883..b4ad086 100644 --- a/jquery.ba-resize.min.js +++ b/jquery.ba-resize.min.js @@ -1,4 +1,4 @@ -/* +/*! * jQuery resize event - v1.1 - 3/14/2010 * http://benalman.com/projects/jquery-resize-plugin/ * @@ -6,4 +6,4 @@ * Dual licensed under the MIT and GPL licenses. * http://benalman.com/about/license/ */ -(function($,h,c){var a=$([]),e=$.resize=$.extend($.resize,{}),i,k="setTimeout",j="resize",d=j+"-special-event",b="delay",f="throttleWindow";e[b]=250;e[f]=true;$.event.special[j]={setup:function(){if(!e[f]&&this[k]){return false}var l=$(this);a=a.add(l);$.data(this,d,{w:l.width(),h:l.height()});if(a.length===1){g()}},teardown:function(){if(!e[f]&&this[k]){return false}var l=$(this);a=a.not(l);l.removeData(d);if(!a.length){clearTimeout(i)}},add:function(l){if(!e[f]&&this[k]){return false}var n;function m(s,o,p){var q=$(this),r=$.data(this,d);r.w=o!==c?o:q.width();r.h=p!==c?p:q.height();n.apply(this,arguments)}if($.isFunction(l)){n=l;return m}else{n=l.handler;l.handler=m}}};function g(){i=h[k](function(){a.each(function(){var n=$(this),m=n.width(),l=n.height(),o=$.data(this,d);if(m!==o.w||l!==o.h){n.trigger(j,[o.w=m,o.h=l])}});g()},e[b])}})(jQuery,this); \ No newline at end of file +(function(b,a,j){function h(){d.each(function(){var f=b(this),a=f.width(),d=f.height(),c=b.data(this,i);if(a!==c.w||d!==c.h)f.trigger(g,[c.w=a,c.h=d])});e!==null&&(e=a.requestAnimationFrame(h))}var d=b([]),c=b.resize=b.extend(b.resize,{}),e,g="resize",i=g+"-special-event";c.delay=250;c.throttleWindow=true;b.event.special[g]={setup:function(){if(!c.throttleWindow&&this.setTimeout)return false;var a=b(this);d=d.add(a);b.data(this,i,{w:a.width(),h:a.height()});d.length===1&&(e=j,h())},teardown:function(){if(!c.throttleWindow&& this.setTimeout)return false;var a=b(this);d=d.not(a);a.removeData(i);d.length||(cancelAnimationFrame(e),e=null)},add:function(a){function d(a,c,f){var g=b(this),h=b.data(this,i);h.w=c!==j?c:g.width();h.h=f!==j?f:g.height();e.apply(this,arguments)}if(!c.throttleWindow&&this.setTimeout)return false;var e;if(b.isFunction(a))return e=a,d;else e=a.handler,a.handler=d}};if(!a.requestAnimationFrame)a.requestAnimationFrame=function(){return a.webkitRequestAnimationFrame||a.mozRequestAnimationFrame||a.oRequestAnimationFrame|| a.msRequestAnimationFrame||function(b){return a.setTimeout(b,c.delay)}}();if(!a.cancelAnimationFrame)a.cancelAnimationFrame=a.webkitCancelRequestAnimationFrame||a.mozCancelRequestAnimationFrame||a.oCancelRequestAnimationFrame||a.msCancelRequestAnimationFrame||clearTimeout})(jQuery,this); \ No newline at end of file