<html> <head> <title>CSS带圆点轮播图</title> <style> .slider { width: 800px; height: 500px; margin: 0 auto; position: relative; overflow: hidden; } .slider .slide { width: 800px; height: 500px; position: absolute; top: 0; display: none; opacity: 0; transition: opacity .5s; } .slider .slide.active { display: block; opacity: 1; } .slider .slider-dots { width: 100%; position: absolute; bottom: 20px; text-align: center; } .slider .slider-dots span { display: inline-block; width: 10px; height: 10px; margin: 0 5px; background-color: #ccc; border-radius: 50%; cursor: pointer; } .slider .slider-dots span.active { background-color: #666; } </style> </head> <body> <div class="slider"> <div class="slide active"><img src="images/slide-1.jpg" alt=""></div> <div class="slide"><img src="images/slide-2.jpg" alt=""></div> <div class="slide"><img src="images/slide-3.jpg" alt=""></div> <div class="slider-dots"> <span class="active"></span> <span></span> <span></span> </div> </div> <script> var slides = document.querySelectorAll('.slide'); var dots = document.querySelectorAll('.slider-dots span'); var currentSlide = 0; function showSlide(n) { if (n= slides.length) { currentSlide = 0; } else { currentSlide = n; } for (var i = 0; i< slides.length; i++) { slides[i].classList.remove('active'); dots[i].classList.remove('active'); } slides[currentSlide].classList.add('active'); dots[currentSlide].classList.add('active'); } function nextSlide() { showSlide(currentSlide + 1); } setInterval(nextSlide, 3000); for (var i = 0; i< dots.length; i++) { (function(i) { dots[i].addEventListener('click', function() { showSlide(i); }); })(i); } </script> </body> </html>
以上代码就是大家完整的带圆点轮播图的CSS代码,其中主要的就是针对轮播图和圆点的样式设计,以及使用JavaScript来实现轮播图的自动播放和点击圆点进行切换图片的功能。通过这些代码,大家可以制作出一个简单却实用的轮播图,给网站页面添加更加醒目的效果。