支持缩放宽度的轮播图 ```html <div class="content-wrap"> <div class="main-wrap"> <div class="main-content"> <div class="lb-wrap"> <!-- 图片 --> <ul class="lb-ul"> <li class="lb-li"> <a href="" class="aimg"> <img src="http://blog.shyngo.cn/article/img/logosg.png" alt=""> </a> <a href="" class="aspan"> <span>今天我做了一个轮播图1</span> </a> </li> <li> <a href="" class="aimg"> <img src="http://blog.shyngo.cn/article/img/logosg.png" alt=""> </a> <a href="" class="aspan"> <span>今天我做了一个轮播图2</span> </a> </li> <li> <a href="" class="aimg"> <img src="http://pic.shyngo.cn/uploads/screenshot/559b9247a44affddebb8543713850e32.png" alt=""> </a> <a href="" class="aspan"> <span>今天我做了一个轮播图3</span> </a> </li> <li> <a href="" class="aimg"> <img src="http://pic.shyngo.cn/uploads/screenshot/c25d5d07ac68f1efabeb32b8f7fbf2d9.png" alt=""> </a> <a href="" class="aspan"> <span>今天我做了一个轮播图4</span> </a> </li> </ul> <!-- 小圆点 --> <ol class="circle"></ol> <!-- 左右侧按钮 --> <a href="javascript:;" class="arrow-l"> <i class="icon iconfont"></i> </a> <a href="javascript:;" class="arrow-r"> <i class="icon iconfont"></i> </a> </div> </div> </div> </div> <script> window.addEventListener('load', function () { // 1.鼠标放到图片上,显示箭头 var outer = document.querySelector('.lb-wrap'); var ul = outer.querySelector('ul'); var lis = outer.querySelector('ul').querySelectorAll('li'); var li = outer.querySelector('ul').querySelectorAll('li')[0]; var li_length = lis.length; var ol = outer.querySelector('ol'); var arrowL = document.querySelector('.arrow-l'); var arrowR = document.querySelector('.arrow-r'); var timer = ''; // 10.自动播放轮播图.(比较像右侧按钮) // var timer = setTimeout(function () { // // 手动调用点击事件 // arrowR.click() // }, 3000); outer.addEventListener('mouseover', function () { arrowL.style.display = 'flex'; arrowR.style.display = 'flex'; // 鼠标经过,清除定时器. clearInterval(timer); timer = null; }); outer.addEventListener('mouseout', function () { arrowL.style.display = 'none'; arrowR.style.display = 'none'; // 鼠标离开,打开定时器 timer = setInterval(function () { // 手动调用点击事件 arrowR.click() }, 2000); }); // 2.动态生成小圆圈.(主要涉及到节点操作.) function generate_circle() { for (var i = 0; i < lis.length; i++) { let li = document.createElement('li'); // 记录当前小圆圈的索引号,通过自定义属性来做. li.setAttribute('index', i); ol.appendChild(li); // 3.小圆圈的排他思想:在生成小圆圈的同时,直接绑定点击事件. li.addEventListener('click', function () { // 除了当前li,其他li都清除掉类名 for (var j = 0; j < ol.children.length; j++) { ol.children[j].className = ''; } this.className = 'current'; // 4.点击小圆圈,滚动图片 var index = this.getAttribute('index'); animate(ul, index); }); } // 把ol中的第一个孩子的类名设置成current ol.children[0].className = 'current'; } generate_circle(); arrowR.addEventListener('click', function () { var left = ul.style.left === "" ? 0 : ul.style.left; var which = (Math.abs(parseInt(left)) / li.clientWidth) console.log("1:", Math.abs(parseInt(left)), "2:", li.clientWidth) animate(ul, which + 1, function () { }); }); arrowL.addEventListener('click', function () { //获取当前是第几个 ul.children.length ul.style.left var left = ul.style.left === "" ? 0 : ul.style.left; var which = Math.abs(parseInt(left)) / li.clientWidth animate(ul, which - 1, function () { }); }); }) function circleChange(index) { var outer = document.querySelector('.lb-wrap'); var ol = outer.querySelector('ol'); for (var i = 0; i < ol.children.length; i++) { ol.children[i].className = ''; } ol.children[index].className = 'current'; } function animate(obj, which, callback) { if (which > obj.querySelectorAll('li').length - 1) { which = 0; } if (which == -1) { which = obj.querySelectorAll('li').length - 1; } obj.style.left = "-" + (which * obj.querySelectorAll('li')[0].clientWidth) + "px"; circleChange(which); } </script> <style> .lb-wrap { position: relative; width: 100%; height: 300px; background-color: pink; overflow: hidden; border-radius: 3px; } .lb-ul { height: 300px; width: 400%; padding: 0; margin: 0; display: flex; flex-direction: row; flex-wrap: nowrap; align-items: center; justify-content: flex-start; position: relative; transition: all 1s linear; } .lb-ul > li { padding: 0; margin: 0; list-style: none; width: 100%; height: 100%; } .lb-ul li .aimg { display: inherit; width: 100%; height: 100%; } .lb-ul li .aimg img { width: 100%; height: 300px; overflow: hidden; object-fit: cover; transition: all 0.2s linear; } .lb-ul li .aspan { position: relative; left: 1rem; height: 2.5rem; line-height: 2.5rem; top: -3.5rem; color: white; } .circle { margin: 0px; padding: 0px; display: flex; position: absolute; top: 90%; right: 1rem; width: 80px; height: 2.5rem; line-height: 2rem; flex-direction: row; flex-wrap: nowrap; align-content: center; justify-content: center; align-items: center; } .lb-wrap .circle li { margin: 0px 3px; list-style: none; color: white; font-size: 2.5rem; cursor: pointer; out-line: none; padding: 0px; float: left; width: 6px; height: 6px; border-radius: 50%; border: 1px solid rgba(86, 85, 83, 0.78); background: rgba(86, 85, 83, 0.78); } .lb-wrap .circle li.current { width: 2rem; border-radius: 3px; border: 1px solid rgba(1, 168, 87, 0.72); background: rgba(1, 168, 87, 0.72); } .arrow-l { z-index: 100; display: none; position: absolute; text-align: right; width: 2rem; height: 40px; background: #583f3f5c; border-radius: 0px 3px 3px 0px; } .arrow-r { z-index: 100; display: none; position: absolute; right: 0px; width: 2rem; height: 40px; background: #583f3f5c; border-radius: 3px 0px 0px 3px; } .arrow-l, .arrow-r { display: none; flex-direction: row; flex-wrap: nowrap; align-content: center; justify-content: center; align-items: center; color: rgba(227, 223, 223, 0.42); top: 47%; } </style> ```