<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <style type="text/css"> #banner { width:100%; padding:0; clear: both; position: relative; height: 400px; z-index:1; } #img-list { display: block; width:5760px; height: 400px; overflow: hidden; z-index:1; position: relative; } #img-list li{ float: left; width:100%; height: 400px; overflow: hidden; /* position: absolute; top:0; left:0; */ z-index: 2; } #list { width:100%; height:18px; text-align: center; position: absolute; bottom: 10px; z-index: 100; } #list > span { width:18px; height: 18px; overflow: hidden; display: inline-block; margin:0 2px; background: url(../images/yuan.png); cursor: pointer; } #list .on { background: url(../images/huanyu.png); } </style> <body> <div id="banner"> <ul id="img-list"> <li><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa0.att.hudong.com%2F30%2F29%2F01300000201438121627296084016.jpg&refer=http%3A%2F%2Fa0.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1617503215&t=76c32e76f33522b5103e35ec6bd8ac67" alt="1"/></li> <li><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa2.att.hudong.com%2F27%2F81%2F01200000194677136358818023076.jpg&refer=http%3A%2F%2Fa2.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1617503215&t=4cf2c362ac2d0004c07357751334ad2a" alt="2"/></li> </ul> <div id="list"> <span index="1" class="on"></span> <span index="2"></span> <span index="3"></span> </div> </div> </body> <script type="text/javascript"> var banner=document.getElementById('banner'); var imglist=document.getElementById('img-list').getElementsByTagName("li"); var list=document.getElementById('list').getElementsByTagName('span'); var index=0; var timer=null; //设置自动播放函数 function autoPlay () { if (++index >= imglist.length) {index = 0}; changeImg(index); } // 鼠标划过整个容器时停止自动播放 banner.onmouseover = function () { clearInterval(timer); } // 鼠标离开整个容器时继续播放至下一张 banner.onmouseout=function(){ timer=setInterval(autoPlay,2000); } // 定义并调用自动播放函数 timer = setInterval(autoPlay, 2000); // 接着定义图片的切换函数 // 定义图片切换函数 function changeImg (curIndex) { for (var i = 0; i < imglist.length; i++) { imglist[i].style.display = "none"; list[i].className = ""; } imglist[curIndex].style.display = "block"; list[curIndex].className = "on"; } for (var i = 0; i < list.length; i++) { list[i].index=i; list[i].onmouseover = function () { clearInterval(timer); changeImg(this.index); }; </script> </html>