Skip to content

Instantly share code, notes, and snippets.

@JoeSz
Created September 24, 2016 14:29
Show Gist options
  • Save JoeSz/e7028d6738bb1633d792cd127dc2115e to your computer and use it in GitHub Desktop.
Save JoeSz/e7028d6738bb1633d792cd127dc2115e to your computer and use it in GitHub Desktop.
Detect Element Height Change (eg. Window)
// Source: http://stackoverflow.com/questions/14866775/detect-document-height-change
var $j = jQuery.noConflict();
$j( document ).ready(function($) {
function onElementHeightChange(elm, callback){
var lastHeight = elm.clientHeight, newHeight;
(function run(){
newHeight = elm.clientHeight;
if( lastHeight != newHeight )
callback();
lastHeight = newHeight;
if( elm.onElementHeightChangeTimer )
clearTimeout(elm.onElementHeightChangeTimer);
elm.onElementHeightChangeTimer = setTimeout(run, 200);
})();
}
onElementHeightChange(document.body, function(){
console.log("height changed");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment