简体   繁体   English

每 30 秒更改一次图像 - 循环

[英]Image change every 30 seconds - loop

I would like to make an image change after 30 seconds.我想在 30 秒后更改图像。 The javascript I'm using looks like this:我正在使用的 javascript 如下所示:

var images = new Array();
images[0] = "image1.jpg";
images[1] = "image2.jpg";
images[2] = "image3.jpg";
setTimeout("changeImage()", 30000);
var x = 0;

function changeImage() {
    document.getElementById("img").src=images[x];
    x++;
}

HTML: HTML:

<img id="img" src="startpicture.jpg">

Now I haven't tested this one yet, but if my calculations are correct it will work :)现在我还没有测试过这个,但如果我的计算是正确的,它将起作用:)

Now what I also want is to make a "fading transition" and I would like the changing of images to loop (it restarts after all the images have been shown).现在我还想要做一个“淡入淡出的过渡”,我希望改变图像循环(在显示所有图像后重新启动)。

Do any of you guys know how to do that?你们有人知道怎么做吗?

I agree with using frameworks for things like this, just because its easier.我同意为这样的事情使用框架,只是因为它更容易。 I hacked this up real quick, just fades an image out and then switches, also will not work in older versions of IE.我很快就解决了这个问题,只是淡出图像然后切换,在旧版本的 IE 中也不起作用。 But as you can see the code for the actual fade is much longer than the JQuery implementation posted by KARASZI István.但是正如您所看到的,实际淡入淡出的代码比 KARASZI István 发布的 JQuery 实现要长得多。

function changeImage() {
    var img = document.getElementById("img");
    img.src = images[x];
    x++;        
    if(x >= images.length) {
        x = 0;
    } 
    fadeImg(img, 100, true);
    setTimeout("changeImage()", 30000);
}

function fadeImg(el, val, fade) {
    if(fade === true) {
        val--;
    } else {
        val ++;
    }       
    if(val > 0 && val < 100) {
        el.style.opacity = val / 100;
        setTimeout(function(){ fadeImg(el, val, fade); }, 10);
    }
}

var images = [], x = 0;
images[0] = "image1.jpg";
images[1] = "image2.jpg";
images[2] = "image3.jpg";
setTimeout("changeImage()", 30000);

You should take a look at various javascript libraries, they should be able to help you out:您应该查看各种 javascript 库,它们应该能够帮助您:

All of them have tutorials, and fade in/fade out is a basic usage.都有教程,淡入淡出是基本用法。

For eg in jQuery:例如在 jQuery 中:

var $img = $("img"), i = 0, speed = 200;
window.setInterval(function() {
  $img.fadeOut(speed, function() {
    $img.attr("src", images[(++i % images.length)]);
    $img.fadeIn(speed);
  });
}, 30000);

I've used this jQuery plugin in the past: 我过去使用过这个jQuery插件:

CrossSlide 拖板

It worked great and does exactly what you want. 它运作良好,完全符合您的要求。

setInterval function is the one that has to be used. setInterval 函数是必须使用的函数。 Here is an example for the same without any fancy fading option.这是相同的示例,没有任何花哨的淡入淡出选项。 Simple Javascript that does an image change every 30 seconds.每 30 秒更改一次图像的简单 Javascript。 I have assumed that the images were kept in a separate images folder and hence _images/ is present at the beginning of every image.我假设图像保存在单独的图像文件夹中,因此 _images/ 出现在每个图像的开头。 You can have your own path as required to be set.您可以根据需要设置自己的路径。

CODE:代码:

var im = document.getElementById("img");

var images = ["_images/image1.jpg","_images/image2.jpg","_images/image3.jpg"];
var index=0;

function changeImage()
{
  im.setAttribute("src", images[index]);
  index++;
  if(index >= images.length)
  {
    index=0;
  }
}

setInterval(changeImage, 30000);

Have a look at innerfade . 看看内在的淡入淡出 Here's an example where I used it to do exactly what you're after, I think. 我想这是一个例子 ,我用它来做你正在做的事情。

Just use That.Its Easy.只需使用它。它很简单。

<script language="javascript" type="text/javascript">
     var images = new Array()
     images[0] = "img1.jpg";
     images[1] = "img2.jpg";
     images[2] = "img3.jpg";
     setInterval("changeImage()", 30000);
     var x=0;

     function changeImage()
     {
                document.getElementById("img").src=images[x]
                x++;
                if (images.length == x) 
                {
                    x = 0;
                }
     }
</script>

And in Body Write this Code:-并在正文中编写此代码:-

<img id="img" src="imgstart.jpg">

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

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