Programming/Javascript

팝업창 중앙에 띄우기

옥수수빵 2020. 11. 19. 12:10
728x90

<script>
$(function() {
    $("a.open-window").on("click", function(e) {
        e.preventDefault();
        const link = $(this).attr("href");
        const popupWidth = 500;
        const popupHeight = 500;
        const screenLeft = window.screenLeft ?? window.screenX;
        const screenTop = window.screenTop ?? window.screenY;
        const screenWidth = window.innerWidth;
        const screenHeight = window.innerHeight;
        const left = screenLeft + (screenWidth - popupWidth) / 2;
        const top = screenTop + (screenHeight - popupHeight) / 2;
        window.open(link, "register", `width=${ popupWidth }, height=${ popupHeight }, top=${ top }, left=${ left }, scrollbars=no`);
    });
});
</script>

<a href="https://naver.com" class="open-window">naver</a>

반응형