This commit is contained in:
jean-emmanuel 2015-09-22 16:02:35 +02:00
parent 42db8a97e7
commit 106d129cae

View file

@ -49,9 +49,9 @@
(function($){
'$:nomunge'; // Used by YUI compressor.
// A jQuery object containing all non-window elements to which the resize
// Array containing all non-window elements to which the resize
// event is bound.
var elems = $([]),
var elems = [],
// Extend $.resize if it already exists, otherwise create it.
jq_resize = $.resize = $.extend( $.resize, {} ),
@ -63,6 +63,8 @@
str_resize = 'resize',
str_data = str_resize + '-special-event',
str_delay = 'delay',
str_pendingDelay = 'pendingDelay',
str_activeDelay = 'activeDelay',
str_throttle = 'throttleWindow';
// Property: jQuery.resize.delay
@ -70,7 +72,9 @@
// The numeric interval (in milliseconds) at which the resize event polling
// loop executes. Defaults to 250.
jq_resize[ str_delay ] = 250;
jq_resize[ str_pendingDelay ] = 250;
jq_resize[ str_activeDelay ] = 20;
jq_resize[ str_delay ] = jq_resize[ str_pendingDelay ];
// Property: jQuery.resize.throttleWindow
//
@ -87,7 +91,7 @@
// event throttling, please note that this property must be changed before any
// window object resize event callbacks are bound.
jq_resize[ str_throttle ] = true;
jq_resize[ str_throttle ] = false;
// Event: resize event
//
@ -139,13 +143,17 @@
var elem = $(this);
// Add this element to the list of internal elements to monitor.
elems = elems.add( elem );
elems.push( this );
// Initialize data store on the element.
$.data( this, str_data, { w: elem.width(), h: elem.height() } );
elem.data( str_data, { w: elem.width(), h: elem.height() } );
// If this is the first element added, start the polling loop.
if ( elems.length === 1 ) {
//set the timeout_id to undefined.
timeout_id = undefined;
//start the loop
loopy();
}
},
@ -161,14 +169,22 @@
var elem = $(this);
// Remove this element from the list of internal elements to monitor.
elems = elems.not( elem );
for (var i = elems.length - 1; i >= 0; i--) {
if(elems[i] == this){
elems.splice(i, 1);
break;
}
};
// Remove any data stored on the element.
elem.removeData( str_data );
// 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;
}
},
@ -190,7 +206,7 @@
function new_handler( e, w, h ) {
var elem = $(this),
data = $.data( this, str_data );
data = elem.data( str_data ) || {};
// If called from the polling loop, w and h will be passed in as
// arguments. If called manually, via .trigger( 'resize' ) or .resize(),
@ -217,30 +233,64 @@
};
function loopy() {
// Start the polling loop, asynchronously.
timeout_id = window[ str_setTimeout ](function(){
// Iterate over all elements to which the 'resize' event is bound.
elems.each(function(){
var elem = $(this),
width = elem.width(),
height = elem.height(),
data = $.data( this, str_data );
for (var i = elems.length - 1; i >= 0; i--) {
var elem = $(elems[i]);
if (elem[0] == window || elem.is(':visible')) {
// If element size has changed since the last time, update the element
// data store and trigger the 'resize' event.
if ( width !== data.w || height !== data.h ) {
elem.trigger( str_resize, [ data.w = width, data.h = height ] );
var width = elem.width(),
height = elem.height(),
data = elem.data( str_data );
// If element size has changed since the last time, update the element
// data store and trigger the 'resize' event.
if ( data && ( width !== data.w || height !== data.h ) ) {
jq_resize[ str_delay ] = jq_resize[ str_activeDelay ];
elem.trigger( str_resize, [ data.w = width, data.h = height ] );
} else {
jq_resize[ str_delay ] = jq_resize[ str_pendingDelay ];
}
}
else {
// resetting stored width and height so that resize event is triggered next time
data = elem.data(str_data);
data.w = 0;
data.h = 0;
}
};
});
// Loop.
loopy();
}, jq_resize[ str_delay ] );
//request another animationFrame to poll the elements
if(timeout_id !== null)
timeout_id = window.requestAnimationFrame(loopy);
};
/**
* 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);