如何使用CSS和JavaScript建立報價幻燈片?


在任何關於想法和語錄的網站上,你都可能見過包含語錄、說出該語錄的名人姓名以及滑塊的語錄幻燈片。此滑塊允許透過點選箭頭鍵之類的單獨按鈕來左右移動幻燈片。讓我們看看如何使用CSS和JavaScript建立一個報價幻燈片。

設定父div

div包含幻燈片、語錄以及上一個和下一個按鈕的容器。語錄使用<h1>元素設定。說出該語錄的名人姓名設定為標題,並進一步設定樣式。所有語錄都設定在子div中:

<div class="slideContainer">
   <div class="Slide">
      <h1>"Self-belief and hard work will always earn you success."</h1>
      <div class="Caption">- Virat Kohli</div>
   </div>
   <div class="Slide">
      <h1>"You don't need a group of superstars, you need a team working together to bring you better results."</h1>
      <div class="Caption">-Brian Lara</div>
   </div>
   <div class="Slide">
      <h1>"I am not thinking too far ahead, just want to take it one thing at a time."</h1>
      <div class="Caption">-Sachin Tendulkar</div>
   </div>
   <a class="prevBtn">❮</a>
   <a class="nextBtn">❯</a>
</div>

設定點狀div

幻燈片上的點/指示器允許你無需點選下一個和上一個按鈕即可導航到幻燈片上的任何幻燈片:

<div style="text-align:center">
   <span class="Navdot" onclick="currentSlide(1)"></span>
   <span class="Navdot" onclick="currentSlide(2)"></span>
   <span class="Navdot" onclick="currentSlide(3)"></span>
</div>

設定幻燈片容器樣式

包含幻燈片的容器div具有最大寬度。位置設定為相對:

.slideContainer {
   max-width: 600px;
   margin: 10px;
   position: relative;
   margin: auto;
}

標題

標題(即實際的語錄)的樣式如下所示。使用font-weight屬性使其看起來更吸引人:

.Caption {
   color: #500070;
   font-weight: bold;
   font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   font-size: 25px;
   padding: 8px 12px;
   position: absolute;
   width: 100%;
   text-align: center;
}

幻燈片的上一頁和下一頁按鈕

幻燈片還包括上一頁和下一頁按鈕,以便於導航。位置設定為絕對。為了正確放置這兩個按鈕,使用了right和left屬性:

prevBtn, .nextBtn {
   position: absolute;
   top : 50%;
   width: auto;
   padding: 10px;
   background-color: rgb(255, 255, 75);
   color: rgb(50, 0, 116);
   font-weight: bolder;
   font-size: 18px;
}
.nextBtn {
   right: -40px;
}
.prevBtn {
   left: -40px;
}

點的動畫

點使幻燈片中每個幻燈片的導航更加容易。使用cursor屬性並將其設定為pointer,以便使用者理解需要點選此屬性進行導航。此外,使用display屬性的inline-block值顯示它。transition屬性允許屬性進行動畫;

.Navdot {
   cursor: pointer;
   height: 15px;
   width: 15px;
   margin: 0 2px;
   background-color: rgb(54, 5, 117);
   border-radius: 50%;
   display: inline-block;
   transition: background-color 0.6s ease;
   display: inline-block;
   margin-top: 30px;
}

示例

要使用CSS和JavaScript建立報價幻燈片,程式碼如下:

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      }
      h1 {
         text-align: center;
      }
      * {
         box-sizing: border-box;
      }
      .Slide {
         display: none;
      }
      img {
         vertical-align: middle;
         width: 100%;
         height: 400px;
      }
      .slideContainer {
         max-width: 600px;
         margin: 10px;
         position: relative;
         margin: auto;
      }
      .prevBtn, .nextBtn {
         position: absolute;
         top: 50%;
         width: auto;
         padding: 10px;
         background-color: rgb(255, 255, 75);
         color: rgb(50, 0, 116);
         font-weight: bolder;
         font-size: 18px;
      }
      .nextBtn {
         right: -40px;
      }
      .prevBtn {
         left: -40px;
      }
      .Caption {
         color: #500070;
         font-weight: bold;
         font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
         font-size: 25px;
         padding: 8px 12px;
         position: absolute;
         width: 100%;
         text-align: center;
      }
      .Navdot {
         cursor: pointer;
         height: 15px;
         width: 15px;
         margin: 0 2px;
         background-color: rgb(54, 5, 117);
         border-radius: 50%;
         display: inline-block;
         transition: background-color 0.6s ease;
         display: inline-block;
         margin-top: 30px;
      }
      .selected, .Navdot:hover {
         background-color: #d9ff00;
      }
      @media only screen and (max-width: 450px) {
         .prevBtn, .nextBtn, .Caption {
            font-size: 16px;
         }
      }
   </style>
</head>
<body>
   <h1>Quote slideShow</h1>
   <hr />
   <div class="slideContainer">
      <div class="Slide">
      <h1>"Self-belief and hard work will always earn you success."</h1>
      <div class="Caption">- Virat Kohli</div>
   </div>
   <div class="Slide">
      <h1>"You don't need a group of superstars, you need a team working together to bring you better results."</h1>
      <div class="Caption">-Brian Lara</div>
   </div>
   <div class="Slide">
      <h1>"I am not thinking too far ahead, just want to take it one thing at a time."</h1>
      <div class="Caption">-Sachin Tendulkar</div>
   </div>
   <a class="prevBtn">❮</a>
   <a class="nextBtn">❯</a>
</div>
<br />
<div style="text-align:center">
   <span class="Navdot" onclick="currentSlide(1)"></span>
   <span class="Navdot" onclick="currentSlide(2)"></span>
   <span class="Navdot" onclick="currentSlide(3)"></span>
</div>
<script>
   document.querySelector(".prevBtn").addEventListener("click", () => {
      changeSlides(-1);
   });
   document.querySelector(".nextBtn").addEventListener("click", () => {
      changeSlides(1);
   });
   var slideIndex = 1;
   showSlides(slideIndex);
   function changeSlides(n) {
      showSlides((slideIndex += n));
   }
   function currentSlide(n) {
      showSlides((slideIndex = n));
   }
   function showSlides(n) {
      var i;
      var slides = document.getElementsByClassName("Slide");
      var dots = document.getElementsByClassName("Navdot");
      if (n > slides.length) {
         slideIndex = 1;
      }
      if (n < 1) {
         slideIndex = slides.length;
      }
      Array.from(slides).forEach(item => (item.style.display = "none"));
      Array.from(dots).forEach(
         item => (item.className = item.className.replace(" selected", ""))
      );
      slides[slideIndex - 1].style.display = "block";
      dots[slideIndex - 1].className += " selected";
   }
</script>
</body>
</html>

更新於:2023年12月8日

941 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告