轮播图也称为焦点图,是网页中比较常见的网页特效。
功能需求:
1.鼠标经过轮播图模块,左右按钮显示,离开隐藏左右按钮。
2.点击右侧按钮一次,图片往左播放一张,以此类推,左侧按钮同理。
3.图片播放的同时,下面小圆圈模块跟随一起变化。
4.点击小圆圈,可以播放相应图片。
5.鼠标不经过轮播图,轮播图也会自动播放图片。
6.鼠标经过,轮播图模块, 自动播放停止。
此文章的代码为一个完整的轮播图的实现代码,复制即可运行
实现效果由于大小问题无法上传,可自行观看淘宝或京东首页轮播图效果
html文件:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta content="width=device-width, initial-scale=1.0"> <title>Document</title> <link href="https://www.jb51.net/css/index.css" > <script src="https://www.jb51.net/js/animate.js"></script> <script src="https://www.jb51.net/js/index.js"></script> </head> <body> <div> <ul> <li><img src="https://www.jb51.net/img/focus.jpg" alt=""></li> <li><img src="https://www.jb51.net/img/focus1.jpg" alt=""></li> <li><img src="https://www.jb51.net/img/focus2.jpg" alt=""></li> <li><img src="https://www.jb51.net/img/focus3.jpg" alt=""></li> </ul> <ol> </ol> <div> < </div> <div> > </div> </div> </body> </html>
css文件:
* { margin: 0; padding: 0; outline: none; } .box { overflow: hidden; position: relative; height: 455px; width: 721px; background-color: pink; margin: 50px auto; } .box ul { position: absolute; top: 0; left: 0; width: 600%; } .box ul li { list-style: none; float: left; } .box ol { position: absolute; bottom: 10px; left: 100px; background-color: rgba(255, 255, 255, .3); border-radius: 8px; list-style: none; height: 10px; padding: 2px 5px; } .box ol li { cursor: pointer; float: left; height: 8px; width: 8px; border: 1px solid #fff; border-radius: 50%; margin: 0 5px; } .arr-l, .arr-r { display: none; color: #fff; cursor: pointer; position: absolute; height: 40px; width: 30px; background-color: rgba(0, 0, 0, .3); line-height: 40px; text-align: center; } .arr-l { top: 50%; left: 0; transform: translateY(-50%); } .arr-r { right: 0; top: 50%; transform: translateY(-50%); } .current { background-color: #fff; }
动画实现js文件animate.js:
function animate(obj, target, callback) { clearInterval(obj.timer); obj.timer = setInterval(function () { if (target - obj.offsetLeft > 0) { var step = Math.ceil((target - obj.offsetLeft) / 10); } else { var step = Math.floor((target - obj.offsetLeft) / 10); } if (obj.offsetLeft == target) { clearInterval(obj.timer); callback && callback(); } obj.style.left = obj.offsetLeft + step + 'px'; }, 15) }
index.js:
window.addEventListener('load', function () { var arrl = this.document.querySelector('.arr-l'); var arrr = this.document.querySelector('.arr-r'); var box = this.document.querySelector('.box'); var boxWidth = box.offsetWidth; box.addEventListener('mouseenter', function () { arrl.style.display = 'block'; arrr.style.display = 'block'; clearInterval(timer); timer = null; }) box.addEventListener('mouseleave', function () { arrl.style.display = 'none'; arrr.style.display = 'none'; timer = setInterval(function () { arrr.click(); }, 2000); }); var ul = box.querySelector('ul'); var ol = box.querySelector('ol'); for (var i = 0; i < ul.children.length; i++) { var li = document.createElement('li'); li.setAttribute('data-index', i); ol.appendChild(li); ol.children[0].className = 'current'; li.addEventListener('click', function () { for (var i = 0; i < ol.children.length; i++) { ol.children[i].className = ''; } this.className = 'current'; var dateindex = this.getAttribute('data-index'); num = dateindex; circle = dateindex; animate(ul, -dateindex * boxWidth); }); } var first = ul.children[0].cloneNode(true); ul.appendChild(first); var num = 0; var circle = 0; var flag = true; arrr.addEventListener('click', function () { if (flag) { flag = false; if (num == ul.children.length - 1) { ul.style.left = 0; num = 0; } num++; animate(ul, -num * boxWidth, function () { flag = true; }); circle++; if (circle == ol.children.length) { circle = 0; } circleChange(); } }); arrl.addEventListener('click', function () { if (flag) { flag = false; if (num == 0) { num = ul.children.length - 1; ul.style.left = -num * boxWidth + 'px'; } num--; animate(ul, -num * boxWidth, function () { flag = true; }); circle--; if (circle < 0) { circle = ol.children.length - 1; } circleChange(); } }); function circleChange() { for (var i = 0; i < ol.children.length; i++) { ol.children[i].className = ''; } ol.children[circle].className = 'current'; } var timer = setInterval(function () { arrr.click(); }, 2000); })
注意事项
引用两个js文件时,animate.js要在index.js之前引用,因为index.js中要用到animate.js里的函数。
精彩专题分享: