如何创建一个Javascript横幅在不同时间切换图片?

发布于 2024-12-28 12:51:41 字数 769 浏览 1 评论 0原文

我有一个横幅,里面有 10 张图片。我知道如何设置超时,以便图片每隔一定的秒数切换一次,但是我如何设置一个计时器来根据我想要单个图片显示的时间来更改图片。

例如: 我希望图片1显示10秒,图片2显示3秒,图片3显示15秒。

到目前为止,这是我的代码:(以 5 秒的相等间隔更改所有图像。Javascript

        window.onload = rotate;        
         var thisAd = 0;        
         var adImages = new Array("Images1/Picture10","Images1/Picture1","Images1  /Picture2","Images1/Picture3","Images1/Picture4","Images1/Picture5","Images1/Picture6","Images1/Picture7","Images1/Picture8","Images1/Picture9");

function rotate(){              
          thisAd++;
          if(thisAd == adImages.lengh){
            thisAd = 0;
          }     
          document.getElementById("adBanner").src = adImages[thisAd];       
          setTimeout(rotate, 5 * 1000);
}

I have a banner that has 10 pictures in it. I know how to set a timeout so the pictures switches every certain amount of seconds but how could I set a timer to change the picture based on how long I want individule ones displayed for.

For example:
I want picture1 displayed for 10 seconds, picture2 displayed for 3 seconds and picture3 displayed for 15 seconds.

This is my code so far: (Which changes the all images at equal intervals of 5 seconds.

Javascript:

        window.onload = rotate;        
         var thisAd = 0;        
         var adImages = new Array("Images1/Picture10","Images1/Picture1","Images1  /Picture2","Images1/Picture3","Images1/Picture4","Images1/Picture5","Images1/Picture6","Images1/Picture7","Images1/Picture8","Images1/Picture9");

function rotate(){              
          thisAd++;
          if(thisAd == adImages.lengh){
            thisAd = 0;
          }     
          document.getElementById("adBanner").src = adImages[thisAd];       
          setTimeout(rotate, 5 * 1000);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不…忘初心 2025-01-04 12:51:41

创建一个时间数组以匹配您的图像。:

var adTimes = new Array(1000,5000,2000.....)

使用计时器中的值

setTimeout(rotate, 5 * adTimes[thisAd]);

Make a times array to match your images.:

var adTimes = new Array(1000,5000,2000.....)

Use the value in the timer

setTimeout(rotate, 5 * adTimes[thisAd]);
娇妻 2025-01-04 12:51:41

您可以将图片存储到具有两个属性的对象中:一个用于 URL,另一个用于延迟,然后使用它。

var adImages = [
    {
        url:"img1.png",
        delay: 5
    },
    {
        url:"img2.png",
        delay: 3
    }
];

然后您可以使用这些属性:

var image = adImages[thisAd];
document.getElementById("adBanner").src = image.url;
setTimeout(rotate, image.delay * 1000);

You could store your pictures into objects with two properties: one for the URL, the other for the delay, then use that.

var adImages = [
    {
        url:"img1.png",
        delay: 5
    },
    {
        url:"img2.png",
        delay: 3
    }
];

Then you can use those properties:

var image = adImages[thisAd];
document.getElementById("adBanner").src = image.url;
setTimeout(rotate, image.delay * 1000);
挽手叙旧 2025-01-04 12:51:41

由于您的计时没有逻辑,因此无法创建通用算法。因此,您必须为每个广告执行 switch / else if。
您还必须在一个周期后重置计数器 thisAd

var time;
if(thisAd>9)thisAd=0;
if(thisAd==0)time=10;
else if(thisAd==1)time=3;
else if(thisAd==2)time=15;
...
setTimeout(rotate, time * 1000);

As your timeing has no logic a general algorithm can not be created. So you have to do a switch / else if for each ad.
You will also have to reset the counter, thisAd, after one cycle.

var time;
if(thisAd>9)thisAd=0;
if(thisAd==0)time=10;
else if(thisAd==1)time=3;
else if(thisAd==2)time=15;
...
setTimeout(rotate, time * 1000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文