简体   繁体   English

我将如何为幻灯片添加自动化?

[英]How would I approach adding automation to a slideshow?

I built a JS slideshow with clickable indicators.我构建了一个带有可点击指示器的 JS 幻灯片。 I would like for my slideshow to play automatically, while maintaining the functionality of the indicator buttons.我希望我的幻灯片自动播放,同时保持指示器按钮的功能。

In the code example below, I attempted to add a setTimeout() method to change the image every 2000ms, but it seems to not be working.在下面的代码示例中,我尝试添加一个setTimeout()方法来每 2000 毫秒更改一次图像,但它似乎不起作用。 What would be the best approach to change the JS so I have some automation?改变 JS 的最佳方法是什么,所以我有一些自动化? I also attempted to use the setInterval method with no luck.我还尝试使用setInterval方法,但没有成功。 I will provide the code below:我将提供以下代码:

Thank you in advance for the help/tips/and advice提前感谢您的帮助/提示/和建议

 var editorialSlideIndex = 1; showEditorialSlides(editorialSlideIndex); //Next/previous controls function plusSlides(n) { showEditorialSlides(editorialSlideIndex += n); } //Thumbnail image controls function currentSlide(n) { showEditorialSlides(editorialSlideIndex = n); } function showEditorialSlides(n) { var i; var slides = document.getElementsByClassName("editorial-slideshow"); var slideDotInd = document.getElementsByClassName("slideDotInd"); if (n > slides.length) { editorialSlideIndex = 1 } if (n < 1) { editorialSlideIndex = slides.length } for (i = 0; i < slides.length; i++) { slides[i].style.display = "none"; } for (i = 0; i < slideDotInd.length; i++) { slideDotInd[i].className = slideDotInd[i].className.replace(" active-slider", ""); } slides[editorialSlideIndex - 1].style.display = "block"; slideDotInd[editorialSlideIndex - 1].className += " active-slider"; setTimeout(showEditorialSlides, 2000); // Change image every 2 seconds }
 /*Slideshow Container */.editorial-slideshow-container { padding-top: 15px; position: relative; margin: auto; } /*Default Hide Images*/.editorial-slideshow { display: none; } /*Next and previous buttons*/.prev, .next { cursor: pointer; position: absolute; top: 50%; width: auto; margin-top: -34px; padding: 10px; color: white; font-weight: bold; font-size: 34px; transition: 0.6s ease; border-radius: 0 3px 3px 0; user-select: none; background-color: rgba(0, 128, 115, .5); } /* Position "next button" */.next { right: 0; border-radius: 3px 0 0 3px; } /* On hover, background color */.prev:hover, .next:hover { background-color: rgba(0, 128, 115, .8); } /* Caption text.text { color: #f2f2f2; font-size: 15px; padding: 8px 12px; position: absolute; bottom: 8px; width: 100%; text-align: center; } Number text (1/3 etc).numbertext { color: #f2f2f2; font-size: 12px; padding: 8px 12px; position: absolute; top: 15; } */ /* The slideslideDotInd/bullets/indicators */.slideDotInd { cursor: pointer; height: 15px; width: 15px; margin: 15px 2px; background-color: #bbb; border-radius: 50%; display: inline-block; transition: background-color 0.6s ease; }.active-slider, .slideDotInd:hover { background-color: #717171; } /* Fading animation */.fade { -webkit-animation-name: fade; -webkit-animation-duration: 1.5s; animation-name: fade; animation-duration: .25s; } @-webkit-keyframes fade { from { opacity: .8 } to { opacity: 1 } } @keyframes fade { from { opacity: .8 } to { opacity: 1 } } @media only screen and (max-width: 425px) { #CGConainer.banner-title H1 { line-height: 50px; } } @media only screen and (min-width: 768px) {.editorial-slideshow-container { width: 75%; /*Max width is 576px*/ } } @media only screen and (min-width: 1024px) {.editorial-slideshow-container { width: 50%; } }
 <div class="editorial-slideshow-container"> <div class="editorial-slideshow fade"> <img src="https://storage.googleapis.com/img.triggermail.io/hammacher/slider-1.jpg" style="width:100%"> </div> <div class="editorial-slideshow fade"> <img src="https://storage.googleapis.com/img.triggermail.io/hammacher/slider-2.jpg" style="width:100%"> </div> <div class="editorial-slideshow fade"> <img src="https://storage.googleapis.com/img.triggermail.io/hammacher/slider-3.jpg" style="width:100%"> </div> <div class="editorial-slideshow fade"> <img src="https://storage.googleapis.com/img.triggermail.io/hammacher/slider-4a.jpg" style="width:100%"> </div> <div class="editorial-slideshow fade"> <img src="https://storage.googleapis.com/img.triggermail.io/hammacher/slider-5a.jpg" style="width:100%"> </div> <div class="editorial-slideshow fade"> <img src="https://storage.googleapis.com/img.triggermail.io/hammacher/slider-6.jpg" style="width:100%"> </div> <div class="editorial-slideshow fade"> <img src="https://storage.googleapis.com/img.triggermail.io/hammacher/slider-7.jpg" style="width:100%"> </div> <div class="editorial-slideshow fade"> <img src="https://storage.googleapis.com/img.triggermail.io/hammacher/slider-8.jpg" style="width:100%"> </div> <div class="editorial-slideshow fade"> <img src="https://storage.googleapis.com/img.triggermail.io/hammacher/slider-9.jpg" style="width:100%"> </div> <div class="editorial-slideshow fade"> <img src="https://storage.googleapis.com/img.triggermail.io/hammacher/slider-10.jpg" style="width:100%"> </div> <p class="prev" onclick="plusSlides(-1)">&lsaquo;</p> <p class="next" onclick="plusSlides(1)">&rsaquo;</p> </div> <div style="text-align:center"> <span class="slideDotInd" onclick="currentSlide(1)"></span> <span class="slideDotInd" onclick="currentSlide(2)"></span> <span class="slideDotInd" onclick="currentSlide(3)"></span> <span class="slideDotInd" onclick="currentSlide(4)"></span> <span class="slideDotInd" onclick="currentSlide(5)"></span> <span class="slideDotInd" onclick="currentSlide(6)"></span> <span class="slideDotInd" onclick="currentSlide(7)"></span> <span class="slideDotInd" onclick="currentSlide(8)"></span> <span class="slideDotInd" onclick="currentSlide(9)"></span> <span class="slideDotInd" onclick="currentSlide(10)"></span> </div>

It's such a small change.这是一个很小的变化。

The way you called setTimeout is wrong.您调用setTimeout的方式是错误的。

function showEditorialSlides(n) {
  var i;
  var slides = document.getElementsByClassName("editorial-slideshow");
  var slideDotInd = document.getElementsByClassName("slideDotInd");
  if (n > slides.length) {
    editorialSlideIndex = 1
  }
  if (n < 1) {
    editorialSlideIndex = slides.length
  }
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";
  }
  for (i = 0; i < slideDotInd.length; i++) {
    slideDotInd[i].className = slideDotInd[i].className.replace(" active-slider", "");
  }
  slides[editorialSlideIndex - 1].style.display = "block";
  slideDotInd[editorialSlideIndex - 1].className += " active-slider";
  setTimeout(() => showEditorialSlides(editorialSlideIndex = (n+1)), 2000); // Change image every 2 seconds
}

And that was the reason your function was being called incorrectly.这就是您的 function 被错误调用的原因。 EDIT: I forgot to mention the reason.编辑:我忘了说原因。 There was no way JavaScript knew what parameters you were passing to the function when you're trying to recurse.当您尝试递归时,JavaScript 无法知道您传递给 function 的参数。

You can try it out in the fiddle您可以在小提琴中尝试一下

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

相关问题 我将如何捕获用户从此幻灯片显示中选择的图像的“ alt”属性? - How would I capture the 'alt' attribute for the images a user selects from this slideshow? 我将如何解决这种曼哈顿天际线/石墙的方法,哪里出了问题? 使用Javascript - How would I fix my approach to this Manhattan Skyline/Stone Wall and where did I go wrong? Javascript 黄瓜/自动化测试:如何使用javascript函数处理整体概念? - Cucumber/Automation testing: How to approach the overall concept using javascript functions? Vegas幻灯片插件-如何延迟幻灯片开始的时间? - Vegas Slideshow Plugin - How can I delay the moment the slideshow starts? 在幻灯片上,如何在幻灯片的每一侧放置箭头? - On the slideshow how can i place the arrows on each side of the slideshow? 如何在指令中添加自定义过滤器? - How would I go about adding a custom filter in a directive? 添加到数字后,如何将数字循环回到开头? - How would I make a number loop back to the start when adding to it? 我想知道这种在javascript中编写可重用代码的方法 - I would like to know this approach of writing reusable code in javascript 在幻灯片上添加按键 - Adding keypress on slideshow 将图像幻灯片添加到页面 - adding slideshow of images to page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM