<style> * { padding: 0; margin: 0; } img { width: 100%; height: 100%; display: block; } ul, ol { list-style: none; } .banner { width: 100%; height: 500px; position: relative; margin: 50px 0; } .banner>ul { width: 100%; height: 100%; position: relative; } .banner>ul>li { width: 100%; height: 100%; position: absolute; left: 0; top: 0; opacity: 0; transition: opacity .5s linear; } .banner>ul>li.active { opacity: 1; } .banner>ol { width: 200px; height: 30px; position: absolute; left: 30px; bottom: 30px; background: rgba(0, 0, 0, .5); display: flex; justify-content: space-around; align-items: center; border-radius: 15px; } .banner>ol>li { width: 20px; height: 20px; border-radius: 50%; background-color: #FFF; cursor: pointer; } .banner>ol>li.active { background-color: orange; } .banner>div { width: 40px; height: 60px; position: absolute; top: 50%; transform: translateY(-50%); background-color: rgba(0, 0, 0, .5); display: flex; justify-content: center; align-items: center; font-size: 30px; color: #FFF; cursor: pointer; } .banner>div.left { left: 0; } .banner>div.right { right: 0; } </style></head><body> <div class="banner"> <ul class="imgbox"> <li class="active"><img src="adv/adv2.jpg" alt=""></li> <li><img src="adv/adv3.jpg" alt=""></li> <li><img src="adv/adv4.jpg" alt=""></li> <li><img src="adv/adv5.jpg" alt=""></li> </ul> <ol> <li data-name="point" data-i="0" class="active"></li> <li data-name="point" data-i="1"></li> <li data-name="point" data-i="2"></li> <li data-name="point" data-i="3"></li> </ol> <div class="left"><</div> <div class="right">></div> </div> <script> // 获取所有li var imgs = document.querySelectorAll('ul>li'); var points = document.querySelectorAll('ol>li'); var banner = document.querySelector('.banner') // 当前表示第几张,默认是0 var index = 0; // 切换一张的函数 // 参数为true,切换下一张. flase,切换上一张,为数字切换索引置顶的那一张 function changeOne(type) { // 让当前这一张消失 imgs[index].classList.remove('active'); points[index].classList.remove('active'); // 根据index传递进来的参数,来切换index的值 if (type == true) { index++; } else if (type == false) { index--; } else { index = type; } // 判定index的边界值 if (index >= imgs.length) { index = 0; } if (index < 0) { index = imgs.length - 1; } // 让改变后的这一张显示出来 imgs[index].className = 'active'; points[index].className = 'active'; } banner.onclick = function (e) { if (e.target.className === 'left') { // console.log('right') changeOne(false) } if (e.target.className === 'right') { // console.log('left') changeOne(true) } if (e.target.dataset.name === 'point') { // console.log('focus') var i = e.target.dataset.i - 0; changeOne(i); } } // 自动切换 var time = setInterval(function () { changeOne(true) }, 3000) </script></body>
jquery实现
<body> <div class="banner"> <ul class="imgbox"> <li class="active"><img src="images/01.png" alt=""></li> <li><img src="images/02.png" alt=""></li> <li><img src="images/03.png" alt=""></li> <li><img src="images/04.png" alt=""></li> <li><img src="images/05.png" alt=""></li> </ul> <ol> <li class="active"></li> <li ></li> <li ></li> <li ></li> <li></li> </ol> <button id="pre"></button> <button id="next"></button> </div> <script> var index = 0; // 点击右键,切换下一张 $("#next").click(function(){ index++; if(index>4){ index = 0; } imgToggle(index,1000) }) // 点击左键,切换下一张 $("#pre").click(function(){ index--; if(index<0){ index = 4; } imgToggle(index,1000) }) // 点击对应焦点,对应图片显示 $('ol li').click(function(){ var num = $(this).index(); // 重置下标 index = num; imgToggle(num,1000); }) // 封装 function imgToggle(index,speed){ $('ol>li').eq(index).addClass('active').siblings().removeClass('active') $('.imgbox li').eq(index).fadeIn(speed).siblings().fadeOut(speed); } function slide(){ time = setInterval(function(){ // 调用右键的函数 $("#next").click(); },2000) } slide(); </script>