轮播图加入定时器
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>京东商城</title> <style> * { margin: 0; padding: 0; list-style: none; } .slider { height: 340px; width: 790px; margin: 100px auto; position: relative; } .slider li { position: absolute; display: none; } .slider li:first-child { display: block; } .arrow { display: none; } .slider:hover .arrow { display: block; } .arrow-left, .arrow-right { font-family: "SimSun", "宋体"; width: 30px; height: 60px; background-color: rgba(0, 0, 0, 0.1); position: absolute; top: 50%; margin-top: -30px; cursor: pointer; text-align: center; line-height: 60px; color: #fff; font-weight: 700; font-size: 30px; } .arrow-left:hover, .arrow-right:hover { background-color: rgba(0, 0, 0, .5); } .arrow-left { left: 0; } .arrow-right { right: 0; } </style> </head> <body> <div class="slider"> <ul> <li><a href="#"><img src="images/1.jpg" alt=""></a></li> <li><a href="#"><img src="images/2.jpg" alt=""></a></li> <li><a href="#"><img src="images/3.jpg" alt=""></a></li> <li><a href="#"><img src="images/4.jpg" alt=""></a></li> <li><a href="#"><img src="images/5.jpg" alt=""></a></li> <li><a href="#"><img src="images/6.jpg" alt=""></a></li> <li><a href="#"><img src="images/7.jpg" alt=""></a></li> <li><a href="#"><img src="images/8.jpg" alt=""></a></li> </ul> <!--箭头--> <div class="arrow"> <span class="arrow-left"><</span> <span class="arrow-right">></span> </div> </div> <script src="jquery-1.12.4.js"></script> <script> $(function () { var $li = $(".slider>ul>li"); var count = 0; var timer =null; var interval =1000 ; $(".arrow-right").click(function () { count++; if(count == $li.length){ count = 0; } $li.eq(count).fadeIn(1000).siblings().fadeOut(1000); }); $(".arrow-left").click(function () { count--; if(count == -1){ count = $li.length - 1; } $li.eq(count).fadeIn(1000).siblings().fadeOut(1000); }); timer=setInterval(function () { $(".arrow-right").trigger("click"); },interval); $(".slider").mouseenter(function () { clearInterval(timer); }); $(".slider").mouseleave(function () { timer=setInterval(function () { $(".arrow-right").trigger("click"); },interval); }); }) </script> </body> </html>
效果:
自动轮播,当鼠标进入该区域的时候停止轮播,当鼠标离开的时候执行自动轮播
笔记转移,由于在有道云的笔记转移,写的时间可能有点久,如果有错误的地方,请指正