简体   繁体   English

标语图片的Javascript递归功能

[英]Javascript recursive function for banner pictures

I trying to create a rotating banner for cycle a few images. 我试图创建循环横幅以循环显示一些图像。

const images = [url1, url2 url3]
const rotate = (url) => {
    document.getElementById('banner').style.background = url
    rotate(next)
}

Url's are correct full paths only shortened here in question. 网址是正确的完整路径,仅在此处有问题。 I'm striggl ing to solve how to get next in code above. 我正在努力解决如何在上面的代码中获得下一步。 This is some code I write from looking at examples. 这是我通过查看示例编写的一些代码。 Im not a js programmer only recently started. 我不是刚刚开始的js程序员。 Thank you for helping. 感谢您的帮助。

Here's one approach: 这是一种方法:

const rotate = (element, delay, urls) => {
    let current = 0;
    (function next() {
        element.style.background = url[current]; // set image
        current = (current + 1) % urls.length;   // update for next pass
        setTimeout(next, delay);                 // recycle
    })();                                        // start immediately
};

with usage: 用法:

rotate(document.getElementById('banner'), 5000, [url1, url2, url3]);

The modulo arithmetic resets current to zero after the last image has been shown. 显示最后一张图像后,模运算将current重置为零。

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

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