如何使用 CSS 和 JavaScript 建立響應式模態圖片?


模態框是一個對話方塊/彈出視窗,顯示在當前頁面的頂部。

要建立響應式模態圖片,我們使用 JavaScript 觸發模態框並在點選時在其中顯示當前圖片。此外,我們還在模態框中使用 alt 屬性作為圖片標題文字。

響應式模態圖片是指根據其大小、裝置解析度、方向和頁面佈局調整大小以適應視窗的圖片。這些圖片通常透過滑鼠點選放大。它們也可以從網路瀏覽器中一鍵下載。

在這個例子中,我們正在建立一個顯示“模態圖片”的網頁。

Example.html

建立一個HTML檔案,在其中定義頁面的結構(檢視)。在這個例子中,我們使用 HTML 程式碼建立當前頁面,其中包含所需的圖片和響應式模態圖片。

<body>
   <h2>Image Modal</h2>
   <img id="myImg" src="https://images.pexels.com/photos/3341110/pexels-photo-3341110.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" alt="Old Home" style="width: 100%; max-width: 300px" />

   <!-- The Modal -->
   <div id="myModal" class="modal">
      <span class="close">×</span>
      <img class="modal-content" id="img01" />
      <div id="caption"></div>
   </div>

Example.css

新增CSS樣式來設定大小和效果。在這個例子中,我們為模態圖片設定樣式,並在每次點選圖片時新增一個效果,使其以全寬全高顯示。

<style>
   body {
      font-family: Arial, Helvetica, sans-serif;
   }

   #myImg {
      border-radius: 5px;
      cursor: pointer;
      transition: 0.3s;
   }

   #myImg:hover {
      opacity: 0.7;
   }

   /* The Modal */

   .modal {
      display: none;
      /* Hidden by default */
      position: fixed;
      /* Stay in place */
      z-index: 1;
      /* Sit on top */
      padding-top: 100px;
      /* Location of the box */
      left: 0;
      top: 0;
      width: 100%;
      /* Full width */
      height: 100%;
      /* Full height */
      overflow: auto;
      /* Enable scroll if needed */
      background-color: rgb(0, 0, 0);
      /* Fallback color */
      background-color: rgba(0, 0, 0, 0.9);
      /* Black w/ opacity */
   }

   /* Modal Content (image) */

   .modal-content {
      margin: auto;
      display: block;
      width: 80%;
      max-width: 700px;
   }

   /* Caption of Modal Image */

   #caption {
      margin: auto;
      display: block;
      width: 80%;
      max-width: 700px;
      text-align: center;
      color: #ccc;
      padding: 10px 0;
      height: 150px;
   }

   /* Add Animation */

   .modal-content,
   #caption {
      -webkit-animation-name: zoom;
      -webkit-animation-duration: 0.6s;
      animation-name: zoom;
      animation-duration: 0.6s;
   }

   @-webkit-keyframes zoom {
      from {
         -webkit-transform: scale(0);
      }
      to {
         -webkit-transform: scale(1);
      }
   }

   @keyframes zoom {
      from {
         transform: scale(0);
      }
      to {
         transform: scale(1);
      }
   }

   /* The Close Button */

   .close {
      position: absolute;
      top: 15px;
      right: 35px;
      color: #f1f1f1;
      font-size: 40px;
      font-weight: bold;
      transition: 0.3s;
   }

   .close:hover,
   .close:focus {
      color: #bbb;
      text-decoration: none;
      cursor: pointer;
   }

   /* 100% Image Width on Smaller Screens */

   @media only screen and (max-width: 700px) {
      .modal-content {
         width: 100%;
      }
   }
</style>

Example.js

使用JavaScript,我們可以執行驗證並在頁面上處理事件。在這個例子中,建立響應式模態圖片,如果點選圖片,則會以全檢視顯示。在 JavaScript 中,使用 DOM 呼叫圖片 src 並設定圖片樣式以顯示塊。

<script>
   // Get the modal
   var modal = document.getElementById("myModal");
   // Get the image and insert it inside the modal - use its "alt" text as a caption
   var img = document.getElementById("myImg");
   var modalImg = document.getElementById("img01");
   var captionText = document.getElementById("caption");
   img.onclick = function() {
      modal.style.display = "block";
      modalImg.src = this.src;
      captionText.innerHTML = this.alt;
   };
   // Get the <span> element that closes the modal
   var span = document.getElementsByClassName("close")[0];
   // When the user clicks on <span> (x), close the modal
   span.onclick = function() {
      modal.style.display = "none";
   };
</script>

完整示例

以下是使用CSS、HTML、JavaScript媒體查詢建立響應式圖片模態框的完整示例。

<!DOCTYPE html>
<html>
<head>
   <meta name="viewport" content="width=device-width, initial-scale=1" />
   <style>
   body {
      font-family: Arial, Helvetica, sans-serif;
   }

   #myImg {
      border-radius: 5px;
      cursor: pointer;
      transition: 0.3s;
   }

   #myImg:hover {
      opacity: 0.7;
   }

   /* The Modal */

   .modal {
      display: none;
      /* Hidden by default */
      position: fixed;
      /* Stay in place */
      z-index: 1;
      /* Sit on top */
      padding-top: 100px;
      /* Location of the box */
      left: 0;
      top: 0;
      width: 100%;
      /* Full width */
      height: 100%;
      /* Full height */
      overflow: auto;
      /* Enable scroll if needed */
      background-color: rgb(0, 0, 0);
      /* Fallback color */
      background-color: rgba(0, 0, 0, 0.9);
      /* Black w/ opacity */
   }

   /* Modal Content (image) */

   .modal-content {
      margin: auto;
      display: block;
      width: 80%;
      max-width: 700px;
   }

   /* Caption of Modal Image */

   #caption {
      margin: auto;
      display: block;
      width: 80%;
      max-width: 700px;
      text-align: center;
      color: #ccc;
      padding: 10px 0;
      height: 150px;
   }

   /* Add Animation */

   .modal-content,
   #caption {
      -webkit-animation-name: zoom;
      -webkit-animation-duration: 0.6s;
      animation-name: zoom;
      animation-duration: 0.6s;
   }

   @-webkit-keyframes zoom {
      from {
         -webkit-transform: scale(0);
      }
      to {
         -webkit-transform: scale(1);
      }
   }

   @keyframes zoom {
      from {
         transform: scale(0);
      }
      to {
         transform: scale(1);
      }
   }

   /* The Close Button */

   .close {
      position: absolute;
      top: 15px;
      right: 35px;
      color: #f1f1f1;
      font-size: 40px;
      font-weight: bold;
      transition: 0.3s;
   }

   .close:hover,
   .close:focus {
      color: #bbb;
      text-decoration: none;
      cursor: pointer;
   }

   /* 100% Image Width on Smaller Screens */

   @media only screen and (max-width: 700px) {
      .modal-content {
         width: 100%;
      }
   }
   </style>
</head>
<body>
   <h2>Image Modal</h2>
   <img id="myImg" src="https://images.pexels.com/photos/3341110/pexels-photo-3341110.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" alt="Old Home" style="width: 100%; max-width: 300px" />
   <!-- The Modal -->
   <div id="myModal" class="modal">
      <span class="close">×</span>
      <img class="modal-content" id="img01" />
      <div id="caption"></div>
   </div>
   <script>
   // Get the modal
   var modal = document.getElementById("myModal");
   // Get the image and insert it inside the modal - use its "alt" text as a caption
   var img = document.getElementById("myImg");
   var modalImg = document.getElementById("img01");
   var captionText = document.getElementById("caption");
   img.onclick = function() {
      modal.style.display = "block";
      modalImg.src = this.src;
      captionText.innerHTML = this.alt;
   };
   // Get the <span> element that closes the modal
   var span = document.getElementsByClassName("close")[0];
   // When the user clicks on <span> (x), close the modal
   span.onclick = function() {
      modal.style.display = "none";
   };
   </script>
</body>
</html>

更新於:2022年12月19日

3K+ 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告