Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

JS Coding

[JavaScript] 모달 직접 만들어 보기 본문

JavaScript

[JavaScript] 모달 직접 만들어 보기

JSKJS 2023. 11. 7. 11:36
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
<style>
    #wrapper {
        margin: 200px auto;  /* 수평 가운데 정렬 */
        width: 70%;
        border: 1px solid black;
    }
    #modal {
        position: fixed;
        left: 0; top: 0;
        width:100%;
        height:100%;
        background-color: rgba(0, 255, 0, 0.7);
        display: none; /* 처음엔 안보여야 하므로 none */
    }
    #modalContent {
        width: 50%;
        height: 80%;
        margin: 50px auto; /* 수평 중앙 정렬 */
        background-color: black;
        color: white;

    }
</style>
</head>
<body>
    <!-- 나의 모달 -->
    <div id="modal">
        <div id="modalContent">
            <h1>난 최고의 프로그래머</h1>
            <button id="clsBtn">X</button>
        </div>
    </div>
    <!-- 보통 메인 영역은 wrapper나 container란 이름을 지정 -->
    <div id="wrapper">
        <button id="modBtn">모달열기</button>
    </div>
<script>
    const modBtn = document.querySelector('#modBtn');
    const clsBtn = document.querySelector('#clsBtn');
    const modal = document.querySelector('#modal');
    modBtn.addEventListener('click', function(){
        modal.style.display = 'block';  // 보이게 한다.
    });

    clsBtn.addEventListener("click",function(){
        modal.style.display = 'none';
    });
</script>
</body>
</html>