轮播图实战
发布于:2022-04-12 09:58:28
次阅读
轮播图
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>轮播图</title> <style> .img { position: absolute; top: 100px; left: 50%; width: 480px; height: 270px; margin-left: -240px; color: #FFFFFF; /*设置图片默认透明度为0 */ opacity: 0; /*设置图片过度效果, 会出现淡入淡出效果 */ transition: all .8s; } .img:nth-child(1){ background: url("/lesson-19/0411/images/1.jpg"); background-size:100%; border-radius: 10px; } .img:nth-child(2){ background: url("/lesson-19/0411/images/2.jpg"); background-size: 100%; border-radius: 10px; } .img:nth-child(3){ background: url("/lesson-19/0411/images/3.jpg"); background-size: 100%; border-radius: 10px; } .prev { position: absolute; top: 185px; left: 50%; width: 20px; height: 30px; border: 1px solid rgba(255, 255, 255, .2); border-radius: 0 5px 5px 0; margin-top: 30px; margin-left: -240px; color: #c2c1cb; text-align: center; background-color: rgba(255, 255, 255, .2); cursor: pointer; } .next { position: absolute; top: 185px; left: 50%; width: 20px; height: 30px; border: 1px solid rgba(255, 255, 255, .2); border-radius: 5px 0 0 5px; margin-top: 30px; margin-left: 218px; color: #c2c1cb; text-align: center; background-color: rgba(255, 255, 255, .2); cursor: pointer; } .next:hover, .prev:hover { background-color: rgba(1, 1, 1, 0.73); color: #FFFFFF; } .div1 { position: absolute; top: 350px; left: 50%; margin-left: -40px; } .little { float: left; width: 10px; height: 10px; border-radius: 100%; background-color: rgba(255, 245, 244, .3); margin-left: 20px; cursor: pointer; border: 1px solid #464646; } .show{ /*通过添加show,改变图片透明度,从而实现切换效果*/ opacity: 1; } .little_show { background-color: #FFFFFF; } </style></head><body> <div id="Box"> <div class="img show"></div> <div class="img"></div> <div class="img"></div> <!--上一张按钮--> <span class="prev"><</span> <!--下一张按钮--> <span class="next">></span> <div class="div1"> <span class="little little_show" data-index="0"></span> <span class="little" data-index="1"></span> <span class="little" data-index="2"></span> </div> </div> <script> let img = document.getElementsByClassName("img"); //获取所有轮播图片 集合 let next = document.querySelector(".next"); //获取下一张 按钮 let prev = document.querySelector(".prev");//获取上一张 按钮 let box = document.getElementById("Box");//获取整个盒子 let little = document.getElementsByClassName("little"); //获取所有轮播圆点按钮 集合 let index = 0; let time = 0;// 重置所有classlet clear = function () { for (let i = 0;i < img.length; i++){ img[i].className = "img"; } for (let i = 0;i < little.length;i++){ little[i].className = "little"; }};let show = function () { img[index].className = "img show"; little[index].className = "little little_show"; time = 0;};// 下一张let nextBtn = function () { clear(); index++; if(index >img.length-1){ index = 0; } show();};next.addEventListener("click",function () { nextBtn();});// 上一张let prevBtn = function () { clear(); index--; if(index < 0){ index = img.length-1; } show();};prev.addEventListener("click",function () { prevBtn();});// 按钮for (let i = 0; i < little.length;i++){ little[i].addEventListener("click",function () { clear(); index = this.getAttribute("data-index"); show(); })}let timer;//全局变量,接收定时器function autoTime(){ //timer接收定时器 timer = setInterval(function () { time++; if(time === 3){ nextBtn(); } else if(time > 3){ time = 0; } // console.log("time = "+time); },500);}// 调用轮播图自动播放方法autoTime();// 当鼠标移入imgBox时,停止轮播,清除定时器box.onmouseover = function () { clearInterval(timer);};// 当鼠标移出imgBox时,重新调用定时器并重置timebox.onmouseout = function () { time = 0; autoTime();}; </script></body></html>
效果
