如何将自动播放插入图像滑块?

How to insert autoplay to image-slider?

我是 Javascript 的新手,想知道如何为我用 HTML、CSS 和 [=31= 创建的这个简单滑块设置自动播放功能].

我希望能够创建的效果是在加载滑块 x 秒后将第 1 张幻灯片更改为第 2 张幻灯片,然后在第 2 张幻灯片之后将第 3 张幻灯片更改为第 2 张幻灯片。此外,我希望幻灯片无限期地更改,并在单击 "next" 或 "previous" 按钮时暂停。

有人可以帮我实现这个吗?

var slideIndex = 1;
showSlides(slideIndex);

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

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

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");
  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";
}

// Animation
.logocontainer {
  width: 800px;
  height: 100px;
  display: flex;
  justify-content: space-around;
  align-items: center;
  background-color: lightblue;
}

.maincontainer {
  width: 800px;
  height: 150px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: lightblue;
}

.dot-container {
  width: 800px;
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: lightblue;
}

.dot {
  cursor: pointer;
  height: 15px;
  width: 15px;
  background-color: #fff;
  border-radius: 50%;
  margin: 5px;
}

.active, .dot:hover {
  background-color: black;
}

.ctacontainer {
  width: 800px;
  height: 100px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: lightblue;
}

.ctacontainer a {
  text-decoration: none;
  background-color: orange;
  padding: 10px 20px 10px 20px;
  border-radius: 3px;
  color: white;
}

.containeritems {
  width: 400px;
  height: 150px;
  display: flex;
  position: relative;
  justify-content: center;
  align-items: center;
}

.mySlides div:nth-child(n) {
  animation-name: fader;
  animation-duration: 1s;
  z-index: 20;
}

@keyframes fader {
  from { opacity: 0.0; }
  to { opacity: 1.0; }
}

.prev, .next {
  cursor: pointer;
  padding: 12px;
  user-select: none;
  background-color: #000;
  color: white;
  margin: 5px;
  border-radius: 3px;
}

.image1, .image2, .image3 {
  display: flex;
  justify-content: center;
  align-items: center; /* OR FLEX-END */
  position: relative;
  color: white;
  width: 700px;
  height: 150px;
}

.image1 {
  background-color: lightgreen;
}

.image2 {
  background-color: lightgrey;
}

.image3 {
  background-color: red;
}
<head>
  <link rel = "stylesheet"
   type = "text/css"
   href = "Simpleslider.css" />
  
<meta name="viewport" content="width=800, initial-scale=1">
  
</head>

<div class="logocontainer">
  <h1>Logo</h1>
</div>

<div class="maincontainer">
  
  <div class="containeritems">
    
    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
    
    <div class="mySlides">
      <div class="image1"><h2>Image 1</h2></div>
     </div>
    <div class="mySlides">
      <div class="image2"><h2>Image 2</h2></div>
     </div>
    <div class="mySlides">
      <div class="image3"><h2>Image 3</h2></div>
     </div>
    
    <a class="next" onclick="plusSlides(1)">&#10095;</a>
    
  </div>
  
</div>

  <div class="dot-container">
  
  <span class="dot" onclick="currentSlide(1)"></span>
  <span class="dot" onclick="currentSlide(2)"></span>
  <span class="dot" onclick="currentSlide(3)"></span>
  
</div>

<div class="ctacontainer">
  <a href="#">Click here</a>
</div>

<!-- INCLUDE JAVASCRIPT-->

<script type="text/javascript" src="Simpleslider.js"></script>

感谢您的帮助!

只需在每个定义的秒后使用 setinterval 到 运行 plusSlides(1),如下所示:

window.setInterval(function(){
    plusSlides(1);
}, 2000); // replace 2000 with the duration you want. Here, 2000 is in ms so the slide will change every 2 seconds.

运行 下面的代码片段可以看到上面的 setInterval 在你的例子中使用:

var slideIndex = 1;
showSlides(slideIndex);

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

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

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");
  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";
}

window.setInterval(function(){
    plusSlides(1);
}, 2000);

// Animation
.logocontainer {
  width: 800px;
  height: 100px;
  display: flex;
  justify-content: space-around;
  align-items: center;
  background-color: lightblue;
}

.maincontainer {
  width: 800px;
  height: 150px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: lightblue;
}

.dot-container {
  width: 800px;
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: lightblue;
}

.dot {
  cursor: pointer;
  height: 15px;
  width: 15px;
  background-color: #fff;
  border-radius: 50%;
  margin: 5px;
}

.active, .dot:hover {
  background-color: black;
}

.ctacontainer {
  width: 800px;
  height: 100px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: lightblue;
}

.ctacontainer a {
  text-decoration: none;
  background-color: orange;
  padding: 10px 20px 10px 20px;
  border-radius: 3px;
  color: white;
}

.containeritems {
  width: 400px;
  height: 150px;
  display: flex;
  position: relative;
  justify-content: center;
  align-items: center;
}

.mySlides div:nth-child(n) {
  animation-name: fader;
  animation-duration: 1s;
  z-index: 20;
}

@keyframes fader {
  from { opacity: 0.0; }
  to { opacity: 1.0; }
}

.prev, .next {
  cursor: pointer;
  padding: 12px;
  user-select: none;
  background-color: #000;
  color: white;
  margin: 5px;
  border-radius: 3px;
}

.image1, .image2, .image3 {
  display: flex;
  justify-content: center;
  align-items: center; /* OR FLEX-END */
  position: relative;
  color: white;
  width: 700px;
  height: 150px;
}

.image1 {
  background-color: lightgreen;
}

.image2 {
  background-color: lightgrey;
}

.image3 {
  background-color: red;
}
<head>
  <link rel = "stylesheet"
   type = "text/css"
   href = "Simpleslider.css" />
  
<meta name="viewport" content="width=800, initial-scale=1">
  
</head>

<div class="logocontainer">
  <h1>Logo</h1>
</div>

<div class="maincontainer">
  
  <div class="containeritems">
    
    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
    
    <div class="mySlides">
      <div class="image1"><h2>Image 1</h2></div>
     </div>
    <div class="mySlides">
      <div class="image2"><h2>Image 2</h2></div>
     </div>
    <div class="mySlides">
      <div class="image3"><h2>Image 3</h2></div>
     </div>
    
    <a class="next" onclick="plusSlides(1)">&#10095;</a>
    
  </div>
  
</div>

  <div class="dot-container">
  
  <span class="dot" onclick="currentSlide(1)"></span>
  <span class="dot" onclick="currentSlide(2)"></span>
  <span class="dot" onclick="currentSlide(3)"></span>
  
</div>

<div class="ctacontainer">
  <a href="#">Click here</a>
</div>

<!-- INCLUDE JAVASCRIPT-->

<script type="text/javascript" src="Simpleslider.js"></script>