Created
March 2, 2015 08:17
-
-
Save vladnicula/e32b7f4c373728e28c3b to your computer and use it in GitHub Desktop.
Custom window resize event
This file contains hidden or 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 $window = $(window), | |
resizeInCooldown = false, | |
resizeTimeout; | |
function onResize() { | |
var resizeValue; | |
// only run this if we didn't run a update 150ms ago or less. | |
if ( !resizeInCooldown ) { | |
// orientation dictates what height and what width means. | |
if (!window.orientation || window.orientation === -90 || window.orientation === 90) { | |
resizeValue = { | |
width : $window.width(), | |
height : $window.height() | |
}; | |
} else { | |
resizeValue = { | |
height : $window.width(), | |
width : $window.height() | |
}; | |
} | |
// trigger the event that should be listened to. This is what you would listen to. | |
$("body").trigger("resized:widnow", [resizeValue]); | |
// start the "in cooldown" functioanlity. This is what we check for at the beginning of the function | |
// to make sure we don't run this code to many times. | |
resizeInCooldown = true; | |
// clear the in cooldown | |
setTimeout( function () { | |
resizeInCooldown = false; | |
}, 150); | |
} else { | |
// if we were in cooldown, we wait a while longer and try to resize again. This is probably | |
// not the best approach, but it sort of works. | |
clearTimeout(resizeTimeout); | |
resizeTimeout = setTimeout( function () { | |
onResize(); | |
}, 200); | |
} | |
} | |
$window.on('resize', onResize); | |
$window.on('orientationchange', function () { | |
$window.trigger("resize"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment