Created
October 27, 2014 08:13
-
-
Save darthmaim/ea1c090a95671a0e034b to your computer and use it in GitHub Desktop.
onResize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var onResize = (function() { | |
var resizeCallbacks = []; | |
var requestedAnimationFrame = false; | |
var supportsRequestedAnimationFrame = "function" === typeof window.requestAnimationFrame; | |
var lastHeight = elements.window.height(), | |
lastWidth = elements.window.width(); | |
var runCallbacks = function() { | |
var callbackLength = resizeCallbacks.length; | |
if( callbackLength === 0 ) { | |
return; | |
} | |
var height = elements.window.height(), | |
width = elements.window.width(); | |
var direction = 0; | |
if( height != lastHeight ) { direction |= registerCallback.DIRECTION_VERTICAL } | |
if( width != lastWidth ) { direction |= registerCallback.DIRECTION_HORIZONTAL } | |
for( var i = 0; i < callbackLength; i++ ) { | |
if( (resizeCallbacks[ i ].direction & direction) !== 0 ) { | |
resizeCallbacks[i].callback.call( null, width, height ); | |
} | |
} | |
lastHeight = height; | |
lastWidth = width; | |
requestedAnimationFrame = false; | |
}; | |
elements.window.on('resize', function() { | |
if( supportsRequestedAnimationFrame ) { | |
if( !requestedAnimationFrame ) { | |
requestedAnimationFrame = true; | |
window.requestAnimationFrame( runCallbacks, document.documentElement ); | |
} | |
} else { | |
runCallbacks(); | |
} | |
}); | |
var registerCallback = function( callback, direction ) { | |
resizeCallbacks.push({ | |
callback: callback, | |
direction: direction || (registerCallback.DIRECTION_VERTICAL + registerCallback.DIRECTION_HORIZONTAL) | |
}); | |
return { | |
runNow: function() { | |
callback.call( null, lastWidth, lastHeight ); | |
} | |
}; | |
}; | |
registerCallback.DIRECTION_VERTICAL = 1; | |
registerCallback.DIRECTION_HORIZONTAL = 2; | |
return registerCallback; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment