简体   繁体   English

自动播放在一页上有多个幻灯片的 javascript 幻灯片不起作用

[英]Autoplaying javascript slideshow that has multiple slideshows on one page not working

I'm trying to make a simple javascript slideshow, that will use prev and next buttons to cycle through, and autoplay.我正在尝试制作一个简单的 javascript 幻灯片,它将使用上一个和下一个按钮循环播放和自动播放。 Plus, require to have multiple slideshows on one page.另外,需要在一页上有多个幻灯片。

Can't get the autoplay on load to work?无法在加载时自动播放工作? Am I missing something?我错过了什么吗?

The code I've been using works with multiple on one page, and prev and next buttons work.我一直在使用的代码在一个页面上使用多个,并且上一个和下一个按钮起作用。 However the auto play doesn't work on the window load, but does start to work when you click one of the previous and next buttons.但是,自动播放在窗口加载时不起作用,但是当您单击上一个和下一个按钮之一时会开始工作。

Thanks.谢谢。

var slideIndex = [1,1,1,1,1]
var slideId = ["mySlides1", "mySlides2", "mySlides3", "mySlides4", "mySlides5"]
showSlides(1, 0);
showSlides(1, 1);
showSlides(1, 2);
showSlides(1, 3);
showSlides(1, 4);

var myTimer;

window.addEventListener("load",function() {
    showSlides(slideIndex[no], no);
    myTimer = setInterval(function(){plusSlides(1, no)}, 4000);
})

function plusSlides(n, no){
  clearInterval(myTimer);
  if (n < 0){
    showSlides(slideIndex[no] -= 1, no);
  } else {
   showSlides(slideIndex[no] += 1, no); 
  }
  if (n === -1){
    myTimer = setInterval(function(){plusSlides(n + 2, no)}, 4000);
  } else {
    myTimer = setInterval(function(){plusSlides(n + 1, no)}, 4000);
  }
}

function currentSlide(n, no){
  clearInterval(myTimer);
  myTimer = setInterval(function(){plusSlides(n + 1, no)}, 4000);
  showSlides(slideIndex[no] = n, no);
}

function showSlides(n, no){
  var i;
  var slides = document.getElementsByClassName(slideId[no]);
  var dotname = "dot" + no; 
  var dots = document.getElementsByName(dotname);
  if (n > slides.length) {slideIndex[no] = 1}
  if (n < 1) {slideIndex[no] = 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_slide", "");
  }
  slides[slideIndex[no]-1].style.display = "block";
  dots[slideIndex[no]-1].className += " active_slide";
}

I suggest you don't reset your interval.我建议你不要重置你的间隔。

If you have an array like so:如果你有一个像这样的数组:

const slides = [
  'slideOne', 'slideTwo',
  'slideThree', 'slideFour',
  'slideFive', 'slideSix'

];

you can just do:你可以这样做:

let autoSliderInterval;
let currentSlideIndex = 0;

function _autoSlide() {
  // Increase Slide index
  currentSlideIndex++;
  // If slides are at the end begin from the start
  if(currentSlideIndex >= slides.length) currentSlideIndex = 0;
  // Display current slide
  displaySlide(currentSlideIndex);
}

Then if you want to acutoscroll just set the timeout:然后如果你想 acutoscroll 只需设置超时:
autoSliderInterval = setInterval(_autoSlide, <your time>);

And if you want to stop auto sliding (for example this could happen when the user clicks on one button) just do:如果您想停止自动滑动(例如,当用户单击一个按钮时可能会发生这种情况),只需执行以下操作:
clearInterval(autoSliderInterval) . clearInterval(autoSliderInterval)

Basic example:基本示例:

 const display = document.getElementById('display'); const button_sliderPrevious = document.getElementById('button_sliderPrevious'); const button_sliderNext = document.getElementById('button_sliderNext'); button_sliderPrevious.addEventListener('click', () => shiftSlide(-1)); button_sliderNext.addEventListener('click', () => shiftSlide(1)); const slides = [ 'slideOne', 'slideTwo', 'slideThree', 'slideFour', 'slideFive', 'slideSix' ]; let autoSliderInterval = setInterval(_autoSlide, 500); let currentSlideIndex = 0; function shiftSlide(amount) { // Stop the inverval if(autoSliderInterval !== null) { clearInterval(autoSliderInterval); autoSliderInterval = null; } // Shift the slide index currentSlideIndex += amount; // Check if below zero (if yes set to max) if(currentSlideIndex < 0) currentSlideIndex = slides.length - 1; // Check if over max (set to zero) if(currentSlideIndex >= slides.length) currentSlideIndex = 0; // Display the slide displaySlide(currentSlideIndex); } function _autoSlide() { // Increase Slide index currentSlideIndex++; // If slides are at the end begin from the start if(currentSlideIndex >= slides.length) currentSlideIndex = 0; // Display current slide displaySlide(currentSlideIndex); } function displaySlide(slideIndex) { display.innerText = slides[slideIndex]; }
 #display { text-align: center; height: 50px; width: 100%; border: 1px solid lightgray; }
 <div id="display"></div> <button id="button_sliderPrevious">Previous</button> <button id="button_sliderNext">Next</button>

Try this one.试试这个。

i hope it will work for you.我希望它对你有用。

<script type="text/javascript">
    var slideIndex = [1,1];
    var slideId = ["mySlides1", "mySlides2"]
    showSlides(1, 0);
    showSlides(1, 1);
    function plusSlides(n, no) {
      showSlides(slideIndex[no] += n, no);
    }
    function showSlides(n, no) {
      var i;
      var x = document.getElementsByClassName(slideId[no]);
      if (n > x.length) {slideIndex[no] = 1}
      if (n < 1) {slideIndex[no] = x.length}
      for (i = 0; i < x.length; i++) {
         x[i].style.display = "none";
      }
      x[slideIndex[no]-1].style.display = "block";
    }
    setInterval(function(){
       $(".next.slide_buttons").click();
    },3000);
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM