1

我正在尝试使用 javascript 创建一个简单的图像幻灯片,但它不起作用。

<script>
var image=document.getElementById("aaa");
var img_array=["images/Chrysanthemum.jpg","images/desert.jpg","images/koala.jpg","images/penguins.jpg","images/hydrangeas.jpg","images/lighthouse.jpg","images/jellyfish.jpg"];
var index=0;
function slide()
{
    image.setAttribute("src",img_array[index]);
    index++;
    if(index>=img_array.length)
    {
    index=0;
    }
}
setInterval("slide()",2000);
</script>
</head>
<body>
<img id="aaa" src="images/tulips.jpg" width="400" height="400" name="image" />
</body>
4

5 回答 5

3

简单的方法:

window.onload = function() {
    var image=document.getElementById("aaa");
    var img_array=[...];
    var index=0;
    var interval = 2000;
    function slide() {
        image.src = img_array[index++%img_array.length];
    }

    setInterval(slide, interval);
}

更好的方法:

更改setInterval(slide,2000);为:

setTimeout(function() {
    slide();
    setTimeout(arguments.callee, interval)
}, interval);
于 2013-02-06T09:48:56.580 回答
2

您好,下面是您的代码的固定版本。

<script>
var image = document.getElementById("aaa");
var img_array=["http://png-4.findicons.com/files/icons/1008/quiet/256/java.png","http://icons.iconarchive.com/icons/tpdkdesign.net/refresh-cl/256/System-Java-icon.png"];
var index=0;
function slide()
{

    document["aaa"].src = img_array[index];
    index++;
    if(index>=img_array.length)
    {
    index=0;
    }
}
setInterval("slide()",2000);
</script>
</head>
<body>
<img id="aaa" src="http://www.tutorialsscripts.com/free-icons/programming-language/java-icons/purple-java-icon-256-x-256.gif" width="400" height="400" name="image" />
于 2013-02-06T09:39:39.640 回答
0

用以下内容替换您的 setinterval 调用

setInterval(function(){slide()},2000);
于 2013-02-06T09:35:35.863 回答
0

代替

setInterval("slide()",2000);

setInterval(slide,2000);
于 2019-01-29T08:41:38.557 回答
-2

代码 :

function slideshow()
{
    var slides= ["background-image : url(image/1.jpg)","background-image : url(image/2.jpg)"];
    var i = 0;
    var divh = document.getElementById("demo").style ;
    while( i < slides.length)
    {           
        if( i == (slides.length-1) )
            {
                divh = slides[i];
                i = 0;
            }
        else
        {
            divh = slides[i];
             i++ ;
        }                      
    }
}
于 2017-09-22T12:34:50.700 回答