轮播图也称为焦点图,是网页中比较常见的网页特效。
功能需求:
"htmlcode">
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="css/index.css" rel="external nofollow" > <script src="/UploadFiles/2021-04-02/animate.js">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里的函数。
精彩专题分享:jQuery图片轮播 JavaScript图片轮播 Bootstrap图片轮播
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
最新资源
- Stravinsky-Symphonies,Volume1-OrquestaSinfon
- 外媒评Switch2:向下兼容是关键 但挑战依然存在
- 任天堂提醒:宠物的尿或唾液或会让NS故障 把它放
- 《博德3》再创新高 Steam掌机总游玩时长近2000年
- 张玮伽《想你的夜DSD》东升 [WAV+CUE][1G]
- 姚璎格《 粤 24KGOLD》正版低速原抓[WAV+CUE][1
- 杨千嬅《如果大家都拥有海》寰亚 [WAV+CUE][998
- 孟庭苇.1994-1990-1994钻石精选集2CD(2022环球X
- 群星.1998-华纳好情歌精选17首【华纳】【WAV+CU
- 张敬轩王菀之.2006-903.ID.CLUB拉阔演奏厅LIVE.