Javascript图像滑动

时间:2014-07-06 11:46:54

标签: javascript

我正在尝试使用java脚本创建图像幻灯片。这是我的代码

 <script type="text/javascript">

//variable that will increment through the images
var step = 1
function slideit() {
    var image1 = ["imges/IMG_0579.JPG","imges/IMG_0580.JPG","imges/IMG_0581.JPG"];
    //var image2 = new Image()

    // var image3 = new Image()

    //if browser does not support the image object, exit.
    document.write("hello");

    document.getElementById("slide").src = image1[step];
    document.write("kjds");

    if (step < 3)
        step++;
    else
        step = 1;
    //call function "slideit()" every 2.5 seconds
    setTimeout("slideit()", 2500);
}
slideit();
//-->
</script>

我收到控制台错误src不为空

这里是img元素

<img id="slide" src="imges/IMG_0588.JPG" />

1 个答案:

答案 0 :(得分:0)

好吧,我不擅长表达,所以我直接给你代码。

<html>
<head>
    <title>test</title>
</head>
<body>
    <img  id="slide" src="#" />
</body>
    <script type="text/javascript">
            var step = 1
            function slideit() {
            var image1 = ["imges/IMG_0579.JPG","imges/IMG_0580.JPG","imges/IMG_0581.JPG"];
            document.getElementById("slide").src = image1[step];
            if (step < 3)
                step++;
            else
                step = 1;
            //call function "slideit()" every 2.5 seconds
            setTimeout("slideit()", 2500);
                }
            slideit();
    </script>
</html>