<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> * { margin: 0; padding: 0; } /* li { list-style: none; } */ .banner { background-color: red; width: 700px; height: 500px; margin: 0 auto; position: relative; } .ul li { width: 700px; height: 500px; font-size: 60px; color: white; line-height: 500px; text-align: center; list-style: none; display: none; } .ol { width: 100%; position: absolute; bottom: 10px; display: flex; justify-content: center; line-height: 30px; } .ol li { list-style: none; width: 20px; height: 20px; background-color: aliceblue; border-radius: 50%; margin: 10px; } .span1 { position: absolute; left: 0; top: 50%; margin-top: -20px; width: 40px; height: 30px; text-align: center; line-height: 30px; background: rgba(0, 0, 0, 0.3) } .span2 { position: absolute; right: 0; top: 50%; margin-top: -20px; width: 40px; height: 30px; line-height: 30px; text-align: center; background: #0000004d } .ul .show { display: block !important; } .ol .active { background-color: green; } </style> </head> <body> <div class="banner"> <ul class="ul"> <li class="show">1</li> <li>2</li> <li>3</li> <li>4</li> </ul> <ol class="ol"> <li class="active"></li> <li></li> <li></li> <li></li> </ol> <span class="span1"> < </span> <span class="span2"> > </span> </div> <script> var oul = document.querySelectorAll('.ul li') var ool = document.querySelectorAll('.ol li') var prev = document.querySelector('.span1') var next = document.querySelector('.span2') var t var index = 0 //实现点击切图换图片 for (var i = 0; i < ool.length; i++) { ool[i].index = i ool[i].onclick = function () { clearInterval(t) for (var j = 0; j < ool.length; j++) { ool[j].classList.remove('active') oul[j].classList.remove('show') } this.classList.add('active') index = this.index //不能var index oul[index].classList.add('show') } } //实现自动换图片 clearInterval(t) t = setInterval(function () { index++ if (index >= ool.length) { index = 0 } for (var j = 0; j < ool.length; j++) { ool[j].classList.remove('active') oul[j].classList.remove('show') } ool[index].classList.add('active') oul[index].classList.add('show') }, 2000) prev.onclick = function () { index-- if (index <= 0) { index = 3 } for (var j = 0; j < ool.length; j++) { ool[j].classList.remove('active') oul[j].classList.remove('show') } ool[index].classList.add('active') oul[index].classList.add('show') } next.onclick = function () { index++ if (index >= 4) { index = 0 } for (var j = 0; j < ool.length; j++) { ool[j].classList.remove('active') oul[j].classList.remove('show') } ool[index].classList.add('active') oul[index].classList.add('show') } </script> </body> </html>