如何使用CSS和JavaScript建立響應式幻燈片畫廊?


在本文中,我們將藉助JavaScript和CSS建立一個響應式幻燈片畫廊。

響應式幻燈片將顯示一系列帶有文字的圖片和兩個箭頭按鈕,您可以使用它們瀏覽圖片。**響應式幻燈片畫廊**或**響應式圖片滑塊畫廊**與響應式圖片幻燈片相同。它是一系列您可以瀏覽的圖片。但它不是使用箭頭標記,而是提供圖片縮圖,在頁面底部以小檢視顯示幻燈片中圖片的預覽。

使用它,您可以更輕鬆地瀏覽圖片,您可以使用縮圖從提供的圖片集中選擇隨機圖片。

以下是建立響應式幻燈片畫廊的步驟。在這個例子中,我們正在建立一個顯示“**響應式幻燈片畫廊**”的網頁。

Example.html

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

<body>
   <h3 style="text-align: center">Slideshow Gallery</h3>
   <div class="container">
      <div class="imgSlides">
         <div class="numbertext">1 / 6</div>
         <img src="https://images.pexels.com/photos/1537979/pexels-photo-1537979.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
      </div>
      <div class="imgSlides">
         <div class="numbertext">2 / 6</div>
         <img src="https://images.pexels.com/photos/59512/pexels-photo-59512.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
      </div>
      <div class="imgSlides">
         <div class="numbertext">3 / 6</div>
         <img src="https://images.pexels.com/photos/2658459/pexels-photo-2658459.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
      </div>
      <div class="imgSlides">
         <div class="numbertext">4 / 6</div>
         <img src="https://images.pexels.com/photos/24821/pexels-photo-24821.jpg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
      </div>
      <div class="imgSlides">
         <div class="numbertext">5 / 6</div>
         <img src="https://images.pexels.com/photos/5689662/pexels-photo-5689662.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
      </div>
      <div class="imgSlides">
         <div class="numbertext">6 / 6</div>
         <img src="https://images.pexels.com/photos/9918913/pexels-photo-9918913.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
      </div>
      <a class="prev" onclick="plusSlides(-1)">❮</a>
      <a class="next" onclick="plusSlides(1)">❯</a>
      <div class="caption-container">
         <p id="caption"></p>
      </div>
      <div class="row">
         <div class="column">
            <img class="demo cursor" src="https://images.pexels.com/photos/1537979/pexels-photo-1537979.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(1)" alt="One" />
         </div>
         <div class="column">
            <img class="demo cursor" src="https://images.pexels.com/photos/59512/pexels-photo-59512.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(2)" alt="Two" />
         </div>
         <div class="column">
            <img class="demo cursor" src="https://images.pexels.com/photos/2658459/pexels-photo-2658459.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(3)" alt="Three" />
         </div>
         <div class="column">
            <img class="demo cursor" src="https://images.pexels.com/photos/24821/pexels-photo-24821.jpg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(4)" alt="Four" />
         </div>
         <div class="column">
            <img class="demo cursor" src="https://images.pexels.com/photos/5689662/pexels-photo-5689662.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(5)" alt="Five" />
         </div>
         <div class="column">
            <img class="demo cursor" src="https://images.pexels.com/photos/9918913/pexels-photo-9918913.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(6)" alt="Six" />
         </div>
      </div>
   </div>

Example.css

新增**CSS樣式**以設定大小和效果。在這個例子中,我們正在為幻燈片設定樣式,並在我們懸停在圖片上時新增懸停效果,使其更清晰。

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

   * {
      box-sizing: border-box;
   }

   img {
      vertical-align: middle;
      height: 400px;
   }

   /* Position the image container */

   .container {
      position: relative;
   }

   /* Hide the images by default */

   .imgSlides {
      display: none;
   }

   .cursor {
      cursor: pointer;
   }

   /* Next & previous buttons */

   .prev,
   .next {
      cursor: pointer;
      position: absolute;
      top: 35%;
      width: auto;
      padding: 16px;
      margin-top: -50px;
      color: white;
      font-weight: bold;
      font-size: 20px;
      border-radius: 0 3px 3px 0;
      user-select: none;
      -webkit-user-select: none;
   }

   /* Position the "next button" to the right */

   .next {
      right: 0;
      border-radius: 3px 0 0 3px;
   }

   /* On hover, add a black background color with a little bit see-through */

   .prev:hover,
   .next:hover {
      background-color: rgba(0, 0, 0, 0.8);
   }

   /* Number text (1/3 etc) */

   .numbertext {
      color: #f2f2f2;
      font-size: 12px;
      padding: 8px 12px;
      position: absolute;
      top: 0;
   }

   /* Container for image text */

   .caption-container {
      text-align: center;
      background-color: #222;
      padding: 2px 16px;
      color: white;
   }

   .row:after {
      content: "";
      display: table;
      clear: both;
   }

   .row img {
      height: 130px;
   }

   /* Six columns side by side */

   .column {
      float: left;
      width: 16.66%;
   }

   /* Add a transparency effect for thumnbail images */

   .demo {
      opacity: 0.6;
   }

   .active,
   .demo:hover {
      opacity: 1;
   }
</style>

Example.js

使用**JavaScript**,我們可以執行驗證並在頁面上處理事件。在這個例子中,我們建立了帶有兩個按鈕**上一個**和**下一個**的響應式幻燈片畫廊。

並將圖片的長度傳遞到JavaScript陣列中,以便迭代從第一個到最後一個圖片連結,或者如果我們點選了圖片,它將以全檢視顯示。

<script>
   let slideIndex = 1;
   showSlides(slideIndex);
   
   function plusSlides(n) {
      showSlides((slideIndex += n));
   }
   
   function currentSlide(n) {
      showSlides((slideIndex = n));
   }
   
   function showSlides(n) {
      let i;
      let slides = document.getElementsByClassName("imgSlides");
      let dots = document.getElementsByClassName("demo");
      let captionText = document.getElementById("caption");
      if (n > slides.length) {
         slideIndex = 1;
      }
      if (n < 1) {
         slideIndex = slides.length;
      }
      for (i = 0; i < slides.length; i++) {
         slides[i].style.display = "none";
      }
      for (i = 0; i < dots.length; i++) {
         dots[i].className = dots[i].className.replace(" active", "");
      }
      slides[slideIndex - 1].style.display = "block";
      dots[slideIndex - 1].className += " active";
      captionText.innerHTML = dots[slideIndex - 1].alt;
   }
</script>

完整示例

以下是使用CSS、HTML、JavaScript和媒體查詢建立響應式幻燈片畫廊的完整示例。

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

   * {
      box-sizing: border-box;
   }

   img {
      vertical-align: middle;
      height: 400px;
   }

   /* Position the image container */

   .container {
      position: relative;
   }

   /* Hide the images by default */

   .imgSlides {
      display: none;
   }

   .cursor {
      cursor: pointer;
   }

   /* Next & previous buttons */

   .prev,
   .next {
      cursor: pointer;
      position: absolute;
      top: 35%;
      width: auto;
      padding: 16px;
      margin-top: -50px;
      color: white;
      font-weight: bold;
      font-size: 20px;
      border-radius: 0 3px 3px 0;
      user-select: none;
      -webkit-user-select: none;
   }

   /* Position the "next button" to the right */

   .next {
      right: 0;
      border-radius: 3px 0 0 3px;
   }

   /* On hover, add a black background color with a little bit see-through */

   .prev:hover,
   .next:hover {
      background-color: rgba(0, 0, 0, 0.8);
   }

   /* Number text (1/3 etc) */

   .numbertext {
      color: #f2f2f2;
      font-size: 12px;
      padding: 8px 12px;
      position: absolute;
      top: 0;
   }

   /* Container for image text */

   .caption-container {
      text-align: center;
      background-color: #222;
      padding: 2px 16px;
      color: white;
   }

   .row:after {
      content: "";
      display: table;
      clear: both;
   }

   .row img {
      height: 130px;
   }

   /* Six columns side by side */

   .column {
      float: left;
      width: 16.66%;
   }

   /* Add a transparency effect for thumnbail images */

   .demo {
      opacity: 0.6;
   }

   .active,
   .demo:hover {
      opacity: 1;
   }
</style>
<body>
   <h3 style="text-align: center">Slideshow Gallery</h3>
   <div class="container">
      <div class="imgSlides">
         <div class="numbertext">1 / 6</div>
         <img src="https://images.pexels.com/photos/1537979/pexels-photo-1537979.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
      </div>
      <div class="imgSlides">
         <div class="numbertext">2 / 6</div>
         <img src="https://images.pexels.com/photos/59512/pexels-photo-59512.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
      </div>
      <div class="imgSlides">
         <div class="numbertext">3 / 6</div>
         <img src="https://images.pexels.com/photos/2658459/pexels-photo-2658459.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
      </div>
      <div class="imgSlides">
         <div class="numbertext">4 / 6</div>
         <img src="https://images.pexels.com/photos/24821/pexels-photo-24821.jpg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
      </div>
      <div class="imgSlides">
         <div class="numbertext">5 / 6</div>
         <img src="https://images.pexels.com/photos/5689662/pexels-photo-5689662.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
      </div>
      <div class="imgSlides">
         <div class="numbertext">6 / 6</div>
         <img src="https://images.pexels.com/photos/9918913/pexels-photo-9918913.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
      </div>
      <a class="prev" onclick="plusSlides(-1)">❮</a>
      <a class="next" onclick="plusSlides(1)">❯</a>
      <div class="caption-container">
         <p id="caption"></p>
      </div>
      <div class="row">
         <div class="column">
            <img class="demo cursor" src="https://images.pexels.com/photos/1537979/pexels-photo-1537979.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(1)" alt="One" />
         </div>
         <div class="column">
            <img class="demo cursor" src="https://images.pexels.com/photos/59512/pexels-photo-59512.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(2)" alt="Two" />
         </div>
         <div class="column">
            <img class="demo cursor" src="https://images.pexels.com/photos/2658459/pexels-photo-2658459.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(3)" alt="Three" />
         </div>
         <div class="column">
            <img class="demo cursor" src="https://images.pexels.com/photos/24821/pexels-photo-24821.jpg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(4)" alt="Four" />
         </div>
         <div class="column">
            <img class="demo cursor" src="https://images.pexels.com/photos/5689662/pexels-photo-5689662.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(5)" alt="Five" />
         </div>
         <div class="column">
            <img class="demo cursor" src="https://images.pexels.com/photos/9918913/pexels-photo-9918913.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(6)" alt="Six" />
         </div>
      </div>
   </div>
   <script>
   let slideIndex = 1;
   showSlides(slideIndex);

   function plusSlides(n) {
      showSlides((slideIndex += n));
   }

   function currentSlide(n) {
      showSlides((slideIndex = n));
   }

   function showSlides(n) {
      let i;
      let slides = document.getElementsByClassName("imgSlides");
      let dots = document.getElementsByClassName("demo");
      let captionText = document.getElementById("caption");
      if (n > slides.length) {
         slideIndex = 1;
      }
      if (n < 1) {
         slideIndex = slides.length;
      }
      for (i = 0; i < slides.length; i++) {
         slides[i].style.display = "none";
      }
      for (i = 0; i < dots.length; i++) {
         dots[i].className = dots[i].className.replace(" active", "");
      }
      slides[slideIndex - 1].style.display = "block";
      dots[slideIndex - 1].className += " active";
      captionText.innerHTML = dots[slideIndex - 1].alt;
   }
   </script>
</body>
</html>

更新於:2022年12月19日

2K+ 瀏覽量

啟動你的職業生涯

透過完成課程獲得認證

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