如何使用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 次浏览

启动你的职业生涯

完成课程获得认证

开始学习
广告