I want to use jQuery to make DIV come out in the center of the screen, what should I do?

Asked 2 years ago, Updated 2 years ago, 41 views

I want to use jQuery to make DIV come out in the center of the screen, what should I do?

javascript jquery html css

2022-09-22 22:04

1 Answers

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.


2022-09-22 22:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.