如何使用 CSS 和 JavaScript 建立彈出視窗?


點選按鈕時會出現一個彈出視窗。在其中新增任何關鍵資訊。要關閉彈出視窗,請在右上角設定一個叉號符號。但是,當滑鼠游標位於網頁上的其他位置時,彈出視窗通常會關閉。

設定一個按鈕來開啟彈出視窗

首先,建立一個使用者點選以開啟彈出視窗的按鈕:

<button class="openPopUp">Open Popup</button>

設定彈出視窗的容器

為彈出視窗設定一個 div。在其中,為彈出視窗內容建立一個子容器。在其中,使用 × HTML 字元程式碼設定關閉符號:

<div class="popUp">
   <div class="popupContent">
      <span class="close">×</span>
      <p>Sample text inside popup</p>
   </div>
</div>

定位彈出視窗

要定位彈出視窗,請使用 position 屬性並將其設定為 fixed。display 屬性設定為 none,因為彈出視窗只有在點選按鈕時才可見,否則保持隱藏:

.popUp {
   text-align: center;
   display: none;
   position: fixed;
   z-index: 1;
   padding-top: 100px;
   left: 0;
   top: 0;
   width: 100%;
   height: 100%;
   background-color: rgba(0, 0, 0, 0.4);
}

彈出視窗內容

在彈出視窗內,訊息區域的寬度設定為 80%:

Within the popup, the message area is set with width 80%:
.popupContent {
   font-size: 20px;
   font-weight: bold;
   background-color: #fefefe;
   margin: auto;
   padding: 20px;
   border: 1px solid #888;
   width: 80%;
}

設定關閉按鈕的樣式

使用 float 屬性將關閉按鈕定位到右側:

.close {
   color: rgb(255, 65, 65);
   float: right;
   font-size: 40px;
   font-weight: bold;
}

懸停關閉按鈕

這樣,當懸停時,cursor 屬性將設定為 pointer,使其看起來可點選:

.close:hover,
.close:focus {
   color: #ff1010;
   cursor: pointer;
}

示例

要使用 CSS 和 JavaScript 建立彈出視窗,程式碼如下:

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         font-family: Arial, Helvetica, sans-serif;
      }
      .popUp {
         text-align: center;
         display: none;
         position: fixed;
         z-index: 1;
         padding-top: 100px;
         left: 0;	
         top: 0;
         width: 100%;
         height: 100%;
         background-color: rgba(0, 0, 0, 0.4);
      }
      .popupContent {
         font-size: 20px;
         font-weight: bold;
         background-color: #fefefe;
         margin: auto;
         padding: 20px;
         border: 1px solid #888;
         width: 80%;
      }
      .close {
         color: rgb(255, 65, 65);
         float: right;
         font-size: 40px;
         font-weight: bold;
      }
      .close:hover,
      .close:focus {
         color: #ff1010;
         cursor: pointer;
      }
   </style>
</head>
<body>
   <h1>Popup Example</h1>
   <button class="openPopUp">Open Popup</button>
   <h2>Click on the above button to open the popup</h2>
   <div class="popUp">
      <div class="popupContent">
         <span class="close">×</span>
         <p>Sample text inside popup</p>
      </div>
   </div>
   <script>
      var popUp = document.querySelector(".popUp");
      var btn = document.querySelector(".openPopUp");
      var span = document.querySelector(".close");
      
      btn.addEventListener("click", () => {
         popUp.style.display = "block";
      });
      
      span.addEventListener("click", () => {
         popUp.style.display = "none";
      });
      
      window.onclick = function(event) {
         if (event.target == popUp) {
            popUp.style.display = "none";
         }
      };
   </script>
</body>
</html>

更新於:2023年12月14日

452 次瀏覽

啟動您的 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.