简体   繁体   English

需要帮助在自动播放的无限幻灯片中实现延迟

[英]Need help implementing a delay in an autoplayed, infinite slideshow

I'm making a looping slideshow that executes when the page loads and continues forever.我正在制作一个循环幻灯片,当页面加载并永远继续时执行。 There are three slides.共有三张幻灯片。 I'd like for each slide to sit there for 5 seconds, then the parent div offsets to the left by -[width of slide]px to show the next slide.我希望每张幻灯片都坐在那里 5 秒钟,然后父 div 向左偏移 -[幻灯片宽度]px 以显示下一张幻灯片。 Initially I made something that logs each offset value, which is good, but when I try implementing the delay, the console spits out all the offset values.最初我做了一些记录每个偏移值的东西,这很好,但是当我尝试实现延迟时,控制台会吐出所有的偏移值。 I expected each one to appear every 5 seconds.我希望每个人每 5 秒出现一次。 I realize this probably isn't a great design, but I'm learning.我意识到这可能不是一个伟大的设计,但我正在学习。

let children = document.getElementsByClassName('div');
let parent = document.getElementById('parent');
let offset = -200;
let loopInterval;

function loop() {
    for (let i = 0; i < children.length; i++) {
        setTimeout(function(){
            let newOffset = offset * i;    
            parent.style.transform = "translateX(" + newOffset + "px)";
            console.log(newOffset);
        }, 5000);
    }
}

function autoplaySlides() {
    loopInterval = setInterval(loop, 0);
}

window.onload = function() {
    autoplaySlides();
}

If the goal is to learn, So let me explain like this:如果目标是学习,那么让我这样解释:

You set 3 different timeOut functions inside the for loop to run 5 seconds later .在 for 循环中设置了 3 个不同的 timeOut 函数以在 5 秒后运行 This will not give you 3 separate delays to run at 5, 10 and 15 seconds.这不会让您在 5、10 和 15 秒时运行 3 个单独的延迟。 Whichever of these is set last, the offset value you will naturally get will also be its multiplier.无论最后设置哪一个,您自然得到的偏移值也将是它的乘数。

So, with this logic, you will have to set up separate halves as 5000 , 10000 and 15000 in the timer.因此,按照这种逻辑,您必须在计时器中分别设置50001000015000的一半。

But I've simplified the code a bit for you.但是我已经为你简化了代码。 This code can help you learn correctly.此代码可以帮助您正确学习。 Please review, test it, and feel free to ask if you get stuck.请查看、测试它,并随时询问您是否遇到问题。

Code Snippet代码片段

 let children = document.getElementsByClassName('slide'); let parent = document.getElementById('parent'); let offset = -200; let loopInterval = 0; function loop() { console.log('Current Slide: ' + loopInterval); loopInterval = children.length - 1 > loopInterval? loopInterval + 1: 0; newOffset = loopInterval * offset; setTimeout(function() { parent.style.transform = "translateX(" + newOffset + "px)"; loop(); }, 5000); } window.onload = loop();
 .wrapper { width: 200px; margin-left: auto; margin-right: auto; overflow: hidden; } #parent { width: 600px; transition: all 1s; }.slide { box-sizing: border-box; height: 100px; padding: 30px; width: 200px; background: pink; float: left; }.slide:nth-child(2) { background: red; color: white; }.slide:nth-child(3) { background: brown; color: white; }
 <div class="wrapper"> <div id="parent"> <div class="slide">Slide 0</div> <div class="slide">Slide 1</div> <div class="slide">Slide 2</div> </div> </div>

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

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