自动JS滑块?

时间:2017-06-13 15:30:27

标签: javascript

我正在努力让js滑块既自动化又有箭头控制,我有箭头控件,但我无法做到这一点,所以它也是自动化的。< / p>

这是代码;

&#13;
&#13;
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<style>
.mySlides {display:none}
.w3-left, .w3-right, .w3-badge {cursor:pointer}
.w3-badge {height:13px;width:13px;padding:0}
</style>


<div class="w3-content w3-display-container" style="max-width:100%">
  <img class="mySlides" src="http://www.rugby-heaven.co.uk/media/wysiwyg/Free_Printing_Lions_Front.jpg" style="width:100%">
  <img class="mySlides" src="http://www.rugby-heaven.co.uk/media/wysiwyg/All_Blacks_Training.jpg" style="width:100%">
  <img class="mySlides" src="http://www.rugby-heaven.co.uk/media/wysiwyg/TourJersey_1.jpg" style="width:100%">
  <div class="w3-center w3-container w3-section w3-large w3-text-white w3-display-bottommiddle" style="width:100%">
    <div class="w3-left w3-hover-text-khaki" onclick="plusDivs(-1)">&#10094;</div>
    <div class="w3-right w3-hover-text-khaki" onclick="plusDivs(1)">&#10095;</div>
    <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(1)"></span>
    <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(2)"></span>
    <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(3)"></span>
  </div>
</div>

<script>
var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
  showDivs(slideIndex += n);
}

function currentDiv(n) {
  showDivs(slideIndex = n);
}

function showDivs(n) {
  var i;
  var x = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("demo");
  if (n > x.length) {slideIndex = 1}    
  if (n < 1) {slideIndex = x.length}
  for (i = 0; i < x.length; i++) {
     x[i].style.display = "none";  
  }
  for (i = 0; i < dots.length; i++) {
     dots[i].className = dots[i].className.replace(" w3-white", "");
  }
  x[slideIndex-1].style.display = "block";  
  dots[slideIndex-1].className += " w3-white";
}
</script>
&#13;
&#13;
&#13;

有人可以让它自动运行吗?

2 个答案:

答案 0 :(得分:0)

您可以通过调用setInterval函数使其自动运行。

  

setInterval()方法以指定的时间间隔(以毫秒为单位)调用函数或计算表达式。

     

setInterval()方法将继续调用函数直到   调用clearInterval(),或关闭窗口。

我在这里创建了一个函数,因此您可以创建一个如何制作它的想法。 您可以为setInterval()

了解更多here

我没有运行此代码,请告知我是否有任何错误。

function makeInfinite(time){
    setInterval(function(){
       plusDivs(1);
    },time*1000);
}

makeInfinite(3);

答案 1 :(得分:0)

setInterval()正是您要找的。您可能还想尝试添加一种方法来延迟每当有人点击不同幻灯片时的间隔,因为点击&#34; slide3&#34;是一个糟糕的用户体验。因为自动化,只能让它在半秒后离开。

&#13;
&#13;
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<style>
.mySlides {display:none}
.w3-left, .w3-right, .w3-badge {cursor:pointer}
.w3-badge {height:13px;width:13px;padding:0}
</style>


<div class="w3-content w3-display-container" style="max-width:100%">
  <img class="mySlides" src="http://www.rugby-heaven.co.uk/media/wysiwyg/Free_Printing_Lions_Front.jpg" style="width:100%">
  <img class="mySlides" src="http://www.rugby-heaven.co.uk/media/wysiwyg/All_Blacks_Training.jpg" style="width:100%">
  <img class="mySlides" src="http://www.rugby-heaven.co.uk/media/wysiwyg/TourJersey_1.jpg" style="width:100%">
  <div class="w3-center w3-container w3-section w3-large w3-text-white w3-display-bottommiddle" style="width:100%">
    <div class="w3-left w3-hover-text-khaki" onclick="plusDivs(-1)">&#10094;</div>
    <div class="w3-right w3-hover-text-khaki" onclick="plusDivs(1)">&#10095;</div>
    <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(1)"></span>
    <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(2)"></span>
    <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(3)"></span>
  </div>
</div>

<script>
var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
  showDivs(slideIndex += n);
}

function currentDiv(n) {
  showDivs(slideIndex = n);
}

function showDivs(n) {
  var i;
  var x = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("demo");
  if (n > x.length) {slideIndex = 1}    
  if (n < 1) {slideIndex = x.length}
  for (i = 0; i < x.length; i++) {
     x[i].style.display = "none";  
  }
  for (i = 0; i < dots.length; i++) {
     dots[i].className = dots[i].className.replace(" w3-white", "");
  }
  x[slideIndex-1].style.display = "block";  
  dots[slideIndex-1].className += " w3-white";
}

setInterval(function(){showDivs(++slideIndex);},3000);
</script>
&#13;
&#13;
&#13;