Skip to content

Instantly share code, notes, and snippets.

@crazyyy
Last active January 5, 2019 18:27
Show Gist options
  • Save crazyyy/223825b936e90cd537a5 to your computer and use it in GitHub Desktop.
Save crazyyy/223825b936e90cd537a5 to your computer and use it in GitHub Desktop.
#js #jquery || get browser width and height on window resize
// get browser width and height on window resize
var headerEl = document.getElementById('header');
// when window size changed - resize first block
window.addEventListener('resize', setWindowSize);
function setWindowSize() {
if (typeof(window.innerWidth) == 'number') {
myWidth = window.innerWidth;
myHeight = window.innerHeight;
setheight(myHeight);
} else {
if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
setheight(myHeight);
} else {
if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
setheight(myHeight);
}
}
}
}
function setheight(height) {
headerEl.style.height = height + 'px';
}
document.addEventListener("DOMContentLoaded", function(event) {
setWindowSize();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment