JS

팝업창 만들기

드비디 2023. 2. 21. 17:05
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>화려한 사이트</title>
  <style>
    #popup-container {
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: rgba(0, 0, 0, 0.5);
      display: none;
    }

    #popup-content {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;
      padding: 20px;
      border-radius: 5px;
      text-align: center;
    }

    #close-btn {
      margin-top: 10px;
    }
  </style>
</head>

<body>
  <button id="popup-btn">팝업 열기</button>

  <div id="popup-container">
    <div id="popup-content">
      <p>팝업 내용</p>
      <button id="close-btn">닫기</button>
    </div>
  </div>
  <script>
    var popupBtn = document.getElementById('popup-btn');
    var closeBtn = document.getElementById('close-btn');
    var popupContainer = document.getElementById('popup-container');

    popupBtn.addEventListener('click', function () {
      popupContainer.style.display = 'block';
    });

    closeBtn.addEventListener('click', function () {
      popupContainer.style.display = 'none';
    });

    document.addEventListener('click', function (event) {
      if (event.target == popupContainer) {
        popupContainer.style.display = 'none';
      }
    });
  </script>
</body>

</html>