var windowHeight = $(window).height();
var windowWidth = $(window).width();
var scrollTop = $(window).scrollTop();
var scrollLeft = $(window).scrollLeft();
jQuery.fn.center = function () {
this.css("position","absolute");
this.css("top", Math.max(0, ((windowHeight - $(this).outerHeight()) / 2) + scrollTop) + "px");
this.css("left", Math.max(0, ((windowWidth - $(this).outerWidth()) / 2) + scrollLeft) + "px");
return this;
}
$('div').center();
I attempted to position the div in the center of the page using a specific function.
However, when the window is resized or users zoom in or out, the function does not update the new position.
Is there a way to execute this function when users resize the window or zoom in or out?