JavaScript对象(document对象 图片轮播)

图片轮播:

需要注意的HTML需要img标签,他和input标签一样,是非封闭的标签

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>广告轮播</title>
<script src="广告轮播.js"></script>
</head>

<body>
<img id="img1" src="image/rose.jpg" />
</body>
</html>

JavaScript:

现在只需要改变JavaScript中数组元素的值即可改变图片

// JavaScript Document
var imageArray = ['apple.jpg','rose.jpg'];

var timer;
var index = 1;
function rotateFunc(){
    var img = document.getElementById("img1");
    img.src = "image/"+imageArray[index];
    
    if(index==imageArray.length-1)
        index = 0;
    else
        index++;
    }

function startRotate(){
    timer = window.setInterval(rotateFunc,1000);
    }
window.addEventListener('load',startRotate,false);

 

posted @ 2016-01-09 20:38  跨七海的风  阅读(261)  评论(0编辑  收藏  举报