From 1d83cc0a063cbc1100f84d6dd94ae4f8502cc6b4 Mon Sep 17 00:00:00 2001 From: unknown <Ezra@Lore.(none)> Date: Wed, 9 Nov 2011 21:53:04 +0100 Subject: [PATCH 1/3] Used requestAnimationFrame for polling. --- jquery.ba-resize.js | 33 +++++++++++++++++++++------------ jquery.ba-resize.min.js | 4 ++-- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/jquery.ba-resize.js b/jquery.ba-resize.js index 1f41d37..30acf44 100644 --- a/jquery.ba-resize.js +++ b/jquery.ba-resize.js @@ -216,11 +216,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 +231,25 @@ } }); - - // Loop. - loopy(); - - }, jq_resize[ str_delay ] ); - + + //request another animationFrame to poll the elements + 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 ) { + window.setTimeout( callback, jq_resize[ str_delay ] ); + }; + })(); + } + +})(jQuery,this); \ No newline at end of file diff --git a/jquery.ba-resize.min.js b/jquery.ba-resize.min.js index c678883..7bd69d9 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(e,b,i){function g(){c.each(function(){var a=e(this),b=a.width(),c=a.height(),d=e.data(this,h);if(b!==d.w||c!==d.h)a.trigger(f,[d.w=b,d.h=c])});b.requestAnimationFrame(g)}var c=e([]),d=e.resize=e.extend(e.resize,{}),f="resize",h=f+"-special-event";d.delay=250;d.throttleWindow=true;e.event.special[f]={setup:function(){if(!d.throttleWindow&&this.setTimeout)return false;var a=e(this);c=c.add(a);e.data(this,h,{w:a.width(),h:a.height()});c.length===1&&g()},teardown:function(){if(!d.throttleWindow&& this.setTimeout)return false;var a=e(this);c=c.not(a);a.removeData(h);c.length||clearTimeout(void 0)},add:function(a){function b(a,d,f){var g=e(this),j=e.data(this,h);j.w=d!==i?d:g.width();j.h=f!==i?f:g.height();c.apply(this,arguments)}if(!d.throttleWindow&&this.setTimeout)return false;var c;if(e.isFunction(a))return c=a,b;else c=a.handler,a.handler=b}};if(!b.requestAnimationFrame)b.requestAnimationFrame=function(){return b.webkitRequestAnimationFrame||b.mozRequestAnimationFrame||b.oRequestAnimationFrame|| b.msRequestAnimationFrame||function(a){b.setTimeout(a,d.delay)}}()})(jQuery,this); \ No newline at end of file From 513a20b2b4e351f46c775f8a4d905211f498d9d2 Mon Sep 17 00:00:00 2001 From: Ezra Pool <ezra@tsdme.nl> Date: Sat, 12 Nov 2011 15:35:08 +0100 Subject: [PATCH 2/3] Add possibility to cancel the animation loop Added a shim for cancelAnimationFrame Updated requestAnimationFrame to return timeout_id Added routine to make sure the loop is stopped (Firefox does not yet implement cancelAnimationFrame) --- jquery.ba-resize.js | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/jquery.ba-resize.js b/jquery.ba-resize.js index 30acf44..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; } }, @@ -233,7 +240,8 @@ }); //request another animationFrame to poll the elements - window.requestAnimationFrame(loopy); + if(timeout_id !== null) + timeout_id = window.requestAnimationFrame(loopy); }; /** @@ -241,15 +249,28 @@ * http://paulirish.com/2011/requestanimationframe-for-smart-animating/ */ if ( !window.requestAnimationFrame ) { - window.requestAnimationFrame = ( function() { - return window.webkitRequestAnimationFrame || - window.mozRequestAnimationFrame || - window.oRequestAnimationFrame || - window.msRequestAnimationFrame || + window.requestAnimationFrame = (function(){ + return window.webkitRequestAnimationFrame || + window.mozRequestAnimationFrame || + window.oRequestAnimationFrame || + window.msRequestAnimationFrame || function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) { - window.setTimeout( callback, jq_resize[ str_delay ] ); + 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 From 66712f3a6d2c94353d08b6dcb3e1141412c2063b Mon Sep 17 00:00:00 2001 From: Ezra Pool <ezra@tsdme.nl> Date: Sat, 12 Nov 2011 15:46:29 +0100 Subject: [PATCH 3/3] update minified code. *Used closure compiler --- jquery.ba-resize.min.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.ba-resize.min.js b/jquery.ba-resize.min.js index 7bd69d9..b4ad086 100644 --- a/jquery.ba-resize.min.js +++ b/jquery.ba-resize.min.js @@ -6,4 +6,4 @@ * Dual licensed under the MIT and GPL licenses. * http://benalman.com/about/license/ */ -(function(e,b,i){function g(){c.each(function(){var a=e(this),b=a.width(),c=a.height(),d=e.data(this,h);if(b!==d.w||c!==d.h)a.trigger(f,[d.w=b,d.h=c])});b.requestAnimationFrame(g)}var c=e([]),d=e.resize=e.extend(e.resize,{}),f="resize",h=f+"-special-event";d.delay=250;d.throttleWindow=true;e.event.special[f]={setup:function(){if(!d.throttleWindow&&this.setTimeout)return false;var a=e(this);c=c.add(a);e.data(this,h,{w:a.width(),h:a.height()});c.length===1&&g()},teardown:function(){if(!d.throttleWindow&& this.setTimeout)return false;var a=e(this);c=c.not(a);a.removeData(h);c.length||clearTimeout(void 0)},add:function(a){function b(a,d,f){var g=e(this),j=e.data(this,h);j.w=d!==i?d:g.width();j.h=f!==i?f:g.height();c.apply(this,arguments)}if(!d.throttleWindow&&this.setTimeout)return false;var c;if(e.isFunction(a))return c=a,b;else c=a.handler,a.handler=b}};if(!b.requestAnimationFrame)b.requestAnimationFrame=function(){return b.webkitRequestAnimationFrame||b.mozRequestAnimationFrame||b.oRequestAnimationFrame|| b.msRequestAnimationFrame||function(a){b.setTimeout(a,d.delay)}}()})(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