功能:

窗口打开背景图自动跳转切换

JavaScript 小案例背景自动切换_html


所有代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>背景切换</title>
</head>
<body>
<img src="img/1.jpg" width="500" id="imgId"/>
</body>
</html>
<script type="text/javascript">
window.onload = function () {

setInterval(step, 1000);
}
var num = 1;
function step() {
if (num < 5) {
num++;
} else {
num = 1;
}
var dom = document.getElementById("imgId");
//更改它images的src属性
dom.src = 'img/' + num + '.jpg';
}

</script>