js之定时器切换图片

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			img{
				width: 600px;
			}
		</style>
		<script type="text/javascript">
			window.onload = function(){
				var img = document.getElementsByTagName('img')[0];
				// console.log(img);
				var arr = ['./images/1.jpg','./images/2.jpg','./images/3.jpg','./images/4.jpg'];
				// console.log(arr);
				var n = 0;
				function qh(){
					n++;
					if(n >= arr.length){
						n = 0;
					}
					console.log(n);
					img.src = arr[n];
				}
				setInterval(qh,2000);
			}
		</script>
	</head>
	<body>
		<div>
			<img src="./images/1.jpg" alt="t">
		</div>
	</body>
</html>

 

posted @ 2020-03-27 12:50  JackieDYH  阅读(27)  评论(0)    收藏  举报  来源