一.要实现的效果
1.点击左右可切换图片
2.点击小圆点 可切换图片
二.效果图
三.代码
1.css
<style type="text/css"> body,img,span,ul,li{margin: 0;padding: 0;} #div1{width: 600px;height: 350px;margin: 150px auto;border: 10px solid #eee;position: relative;} img{width: 600px;height: 350px;} span{display: inline-block;position: absolute;width: 50px;height: 50px;border-radius: 25px;background:rgba(0,0,0,.3); font-size: 30px;line-height: 50px;text-align: center;cursor: pointer;color: white;} span:hover{background:rgba(255,255,255,.5);color:black;transform:scale(1.3);} span#L{left: 10px;top: 150px;} span#R{right: 10px;top: 150px;} #div1 #ul{position: absolute;bottom: 10px;left: 250px;width: 125px;height: 20px;} #div1 #ul li{list-style: none;float: left;height: 20px;width: 20px;border-radius: 10px;background:rgba(255,255,255,.5); margin-right: 5px;cursor: pointer;} #div1 #ul .active{background:rgba(0,0,0,.5);} </style>
2.html
<body> <div id="div1"> <img src="/UploadFiles/2021-04-02/">3.js
<script type="text/javascript"> window.onload=function(){ var div1=document.getElementById('div1'); var oImg=div1.getElementsByTagName('img')[0]; var spanL=document.getElementById('L'); var spanR=document.getElementById('R'); var oUl=document.getElementById('ul'); var oLi=oUl.getElementsByTagName('li'); var arrImg=['img2/1.jpg','img2/2.jpg','img2/3.jpg','img2/4.jpg','img2/5.jpg']; var num=0; var oldLi=0; function fn(num) { oImg.src=arrImg[num]; } fn(0); function fnLi(num){ oLi[oldLi].className=''; oLi[num].className='active'; oldLi=num; } fnLi(0); /*点击左右span图片切换*/ /*点击左右span时li切换*/ spanL.onclick=function(){ if (num<1) { num=arrImg.length-1; fn(num); fnLi(num); }else{ num--; fnLi(num); fn(num); } } spanR.onclick=function(){ if (num==arrImg.length-1) { num=0; fn(num); fnLi(num); }else{ num++; fnLi(num); fn(num); } } /*点击li实现图片切换*/ for (var i = 0; i < oLi.length; i++) { oLi[i].index=i; oLi[i].onclick=function(){ fn(this.index); fnLi(this.index); } } } </script>总结
以上所述是小编给大家介绍的JS 实现banner图片轮播效果(鼠标事件),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!