I want to use jQuery to make DIV come out in the center of the screen, what should I do?
javascript jquery html css
jQuery.fn.center = function () {
this.css("position","absolute");
this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) +
$(window).scrollTop()) + "px");
this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) +
$(window).scrollLeft()) + "px");
return this;
}
So I made it into a function.
$(element).center();
You can write it like this.
© 2024 OneMinuteCode. All rights reserved.