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

时间:2021-08-08 22:24:44

图片轮播:

需要注意的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="data: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);