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

时间:2018-12-17 11:05:53

标签: javascript html css slider autoplay

我对Java脚本还很陌生,并且想知道如何为我使用HTML,CSS和Javascript创建的此简单滑块设置自动播放功能。

我想创建的效果是,在加载滑块后的x秒后将第一张幻灯片更改为第二张幻灯片,然后在第二张之后将第三张幻灯片更改为第二张幻灯片。另外,我希望幻灯片可以无限期更改,并在单击“下一个”或“上一个”-按钮时暂停。

有人可以帮我实现这一目标吗?

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>

谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

仅在每个定义的秒之后使用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.

运行下面的代码片段,以查看示例{l中使用的上述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>