I have created a small script to center a form vertically using js. However, it does not seem to work with window.onresize and I dont understand why. BTW it works perfect on window.onload and the window.onresize will fire an alert on resize if I ask it to. Here is my code:
var resetHeight = function(){
var loginForm = document.querySelector('.login-form');
var windowHeight = window.innerHeight;
var topNavbarHeight = document.querySelector('.top-navbar').clientHeight;
var loginFormHeight = loginForm.clientHeight;
var footerHeight = document.querySelector('footer').clientHeight;
var totalHeights = loginFormHeight + topNavbarHeight + footerHeight + 90;
var halfHeight = (windowHeight-totalHeights) / 2;
loginForm.style.paddingTop = halfHeight + "px";
loginForm.style.paddingBottom = halfHeight+ "px";
};
window.onload = resetHeight;
window.onresize = resetHeight;
Have you tried alerting your required values upon window resize? You should give the JQuery solution a try:
Also, I would try using the
onresize="resetHeight()"event listener inside the tag of your HTML. Let me know if this works!