jQuery를 써서 DIV가 화면 중앙에 나오게 하고싶은데 어떻게 하면 좋을까요?

조회수 2733회

jQuery를 써서 DIV가 화면 중앙에 나오게 하고싶은데 어떻게 하면 좋을까요?

1 답변

  • 좋아요

    0

    싫어요
    채택 취소하기
    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;
    }
    

    이런식으로 함수로 만들어봤습니다.

    $(element).center(); 이렇게 쓰시면 됩니다.

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)