如何实现轮播图 Slideshow
本教程将学习如何使用CSS和JavaScript创建一个轮播图组件。
轮播图
轮播图,在英语中称为Slideshow,或是Carousel,用于循环展示图片,文字,或是视频。
我们先看一个例子:
轮播图有什么功能?
通过上面的例子,我们想一想,一个轮播图包含什么功能呢?
由有几张图片组成
头部有当前是第几张的序号,底部有当前图片位置的小按钮
左右两边是上一张下一张的按钮
创建一个轮播图
步骤1,创建HTML
增加图片,序号,切换按钮的HTML
<!-- 创建一个容器包含图片内容 --> <div class="slideshow-container"> <!-- 第一张图片 --> <div class="mySlides fade"> <div class="numbertext">1 / 3</div> <img src="img1.jpg" style="width:100%"> <div class="text">标题1</div> </div> <div class="mySlides fade"> <div class="numbertext">2 / 3</div> <img src="img2.jpg" style="width:100%"> <div class="text">标题2</div> </div> <div class="mySlides fade"> <div class="numbertext">3 / 3</div> <img src="img3.jpg" style="width:100%"> <div class="text">标题3</div> </div> <!-- 上一张/下一张的按钮 --> <a class="prev" onclick="plusSlides(-1)">❮</a> <a class="next" onclick="plusSlides(1)">❯</a> </div> <br> <!-- 底部的指示按钮 --> <div style="text-align:center"> <span class="dot" onclick="currentSlide(1)"></span> <span class="dot" onclick="currentSlide(2)"></span> <span class="dot" onclick="currentSlide(3)"></span> </div>
步骤2 添加CSS:
为上一张,下一张的按钮,还有标题,指示按钮增加样式:
* {box-sizing:border-box} /* 轮播图容器 */ .slideshow-container { max-width: 1000px; position: relative; margin: auto; } .mySlides { display: none; } /* 上一张和下一张的按钮 */ .prev, .next { cursor: pointer; position: absolute; top: 50%; width: auto; margin-top: -22px; padding: 16px; color: white; font-weight: bold; font-size: 18px; transition: 0.6s ease; border-radius: 0 3px 3px 0; } /* 将”下一张“的按钮定位在右边 */ .next { right: 0; border-radius: 3px 0 0 3px; } /* 鼠标移到按钮上,增加一个有点透明的黑色背景 */ .prev:hover, .next:hover { background-color: rgba(0,0,0,0.8); } /* 标题的文本 */ .text { color: #f2f2f2; font-size: 15px; padding: 8px 12px; position: absolute; bottom: 8px; width: 100%; text-align: center; } /* 图片顺序的文本,(如,1/3) */ .numbertext { color: #f2f2f2; font-size: 12px; padding: 8px 12px; position: absolute; top: 0; } /* 底部的指示按钮 */ .dot { cursor:pointer; height: 13px; width: 13px; margin: 0 2px; background-color: #bbb; border-radius: 50%; display: inline-block; transition: background-color 0.6s ease; } .active, .dot:hover { background-color: #717171; } /* 淡入淡出的动画 */ .fade { -webkit-animation-name: fade; -webkit-animation-duration: 1.5s; animation-name: fade; animation-duration: 1.5s; } @-webkit-keyframes fade { from {opacity: .4} to {opacity: 1} } @keyframes fade { from {opacity: .4} to {opacity: 1} }
步骤3:增加JavaScript:
实现点击按钮,会跳转到当前的幻灯片
var slideIndex = 1; showSlides(slideIndex); // 展示上一张/下一 function plusSlides(n) { showSlides(slideIndex += n); } // 展示并记录指定位置的图片 function currentSlide(n) { showSlides(slideIndex = n); } // 展示指定位置的图片 function showSlides(n) { var i; var slides = document.getElementsByClassName("mySlides"); var dots = document.getElementsByClassName("dot"); if (n > slides.length) { slideIndex = 1 } if (n < 1) { slideIndex = slides.length } for (i = 0; i < slides.length; i++) { slides[i].style.display = "none"; } for (i = 0; i < dots.length; i++) { dots[i].className = dots[i].className.replace(" active", ""); } slides[slideIndex - 1].style.display = "block"; dots[slideIndex - 1].className += " active"; }
最终轮播图的效果如下
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> * { box-sizing: border-box } body { font-family: Verdana, sans-serif; margin: 0 } .mySlides { display: none } /* 轮播图容器 */ .slideshow-container { max-width: 1000px; position: relative; margin: auto; } /* 上一张和下一张的按钮 */ .prev, .next { cursor: pointer; position: absolute; top: 50%; width: auto; padding: 16px; margin-top: -22px; color: white; font-weight: bold; font-size: 18px; transition: 0.6s ease; border-radius: 0 3px 3px 0; } /* 将”下一张“的按钮定位在右边 */ .next { right: 0; border-radius: 3px 0 0 3px; } /* 鼠标移到按钮上,增加一个有点透明的黑色背景 */ .prev:hover, .next:hover { background-color: rgba(0, 0, 0, 0.8); } /* 标题的文本 */ .text { color: #f2f2f2; font-size: 15px; padding: 8px 12px; position: absolute; bottom: 8px; width: 100%; text-align: center; } /* 图片顺序的文本,(如,1/3) */ .numbertext { color: #f2f2f2; font-size: 12px; padding: 8px 12px; position: absolute; top: 0; } /* 底部的指示按钮 */ .dot { cursor: pointer; height: 13px; width: 13px; margin: 0 2px; background-color: #bbb; border-radius: 50%; display: inline-block; transition: background-color 0.6s ease; } .active, .dot:hover { background-color: #717171; } /* 淡入淡出的动画 */ .fade { -webkit-animation-name: fade; -webkit-animation-duration: 1.5s; animation-name: fade; animation-duration: 1.5s; } @-webkit-keyframes fade { from { opacity: .4 } to { opacity: 1 } } @keyframes fade { from { opacity: .4 } to { opacity: 1 } } </style> </head> <body> <div class="slideshow-container"> <div class="mySlides fade"> <div class="numbertext">1 / 3</div> <img src="https://image-10001577.image.myqcloud.com/upload/3/20170428/14933734123710.jpg" style="width:100%"> <div class="text">标题1</div> </div> <div class="mySlides fade"> <div class="numbertext">2 / 3</div> <img src="https://image-10001577.image.myqcloud.com/upload/3/20170428/1493373435281.jpg" style="width:100%"> <div class="text">标题2</div> </div> <div class="mySlides fade"> <div class="numbertext">3 / 3</div> <img src="https://image-10001577.image.myqcloud.com/upload/3/20170428/14933734551830.jpg" style="width:100%"> <div class="text">标题3</div> </div> <a class="prev" onclick="plusSlides(-1)">❮</a> <a class="next" onclick="plusSlides(1)">❯</a> </div> <br> <div style="text-align:center"> <span class="dot" onclick="currentSlide(1)"></span> <span class="dot" onclick="currentSlide(2)"></span> <span class="dot" onclick="currentSlide(3)"></span> </div> <script> var slideIndex = 1; showSlides(slideIndex); function plusSlides(n) { showSlides(slideIndex += n); } function currentSlide(n) { showSlides(slideIndex = n); } function showSlides(n) { var i; var slides = document.getElementsByClassName("mySlides"); var dots = document.getElementsByClassName("dot"); if (n > slides.length) { slideIndex = 1 } if (n < 1) { slideIndex = slides.length } for (i = 0; i < slides.length; i++) { slides[i].style.display = "none"; } for (i = 0; i < dots.length; i++) { dots[i].className = dots[i].className.replace(" active", ""); } slides[slideIndex - 1].style.display = "block"; dots[slideIndex - 1].className += " active"; } </script> </body> </html>
让幻灯片动起来
要让幻灯片自动切换,可以通过定时器来实现
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> * { box-sizing: border-box } body { font-family: Verdana, sans-serif; } .mySlides { display: none } /* 轮播图容器 */ .slideshow-container { max-width: 1000px; position: relative; margin: auto; } /* 上一张和下一张的按钮 */ .prev, .next { cursor: pointer; position: absolute; top: 50%; width: auto; padding: 16px; margin-top: -22px; color: white; font-weight: bold; font-size: 18px; transition: 0.6s ease; border-radius: 0 3px 3px 0; } /* 将”下一张“的按钮定位在右边 */ .next { right: 0; border-radius: 3px 0 0 3px; } /* 鼠标移到按钮上,增加一个有点透明的黑色背景 */ .prev:hover, .next:hover { background-color: rgba(0, 0, 0, 0.8); } /* 标题的文本 */ .text { color: #f2f2f2; font-size: 15px; padding: 8px 12px; position: absolute; bottom: 8px; width: 100%; text-align: center; } /* 图片顺序的文本,(如,1/3) */ .numbertext { color: #f2f2f2; font-size: 12px; padding: 8px 12px; position: absolute; top: 0; } /* 底部的指示按钮 */ .dot { cursor: pointer; height: 13px; width: 13px; margin: 0 2px; background-color: #bbb; border-radius: 50%; display: inline-block; transition: background-color 0.6s ease; } .active, .dot:hover { background-color: #717171; } /* 淡入淡出的动画 */ .fade { -webkit-animation-name: fade; -webkit-animation-duration: 1.5s; animation-name: fade; animation-duration: 1.5s; } @-webkit-keyframes fade { from { opacity: .4 } to { opacity: 1 } } @keyframes fade { from { opacity: .4 } to { opacity: 1 } } </style> </head> <body> <h2>轮播图自动切换</h2> <p>每两秒更改到下一张图片:</p> <div class="slideshow-container"> <div class="mySlides fade"> <div class="numbertext">1 / 3</div> <img src="https://image-10001577.image.myqcloud.com/upload/3/20170428/14933734123710.jpg" style="width:100%"> <div class="text">标题1</div> </div> <div class="mySlides fade"> <div class="numbertext">2 / 3</div> <img src="https://image-10001577.image.myqcloud.com/upload/3/20170428/1493373435281.jpg" style="width:100%"> <div class="text">标题2</div> </div> <div class="mySlides fade"> <div class="numbertext">3 / 3</div> <img src="https://image-10001577.image.myqcloud.com/upload/3/20170428/14933734551830.jpg" style="width:100%"> <div class="text">标题3</div> </div> </div> <br> <div style="text-align:center"> <span class="dot"></span> <span class="dot"></span> <span class="dot"></span> </div> <script> var slideIndex = 0; showSlides(); function showSlides() { var i; var slides = document.getElementsByClassName("mySlides"); var dots = document.getElementsByClassName("dot"); for (i = 0; i < slides.length; i++) { slides[i].style.display = "none"; } slideIndex++; if (slideIndex > slides.length) { slideIndex = 1 } for (i = 0; i < dots.length; i++) { dots[i].className = dots[i].className.replace(" active", ""); } slides[slideIndex - 1].style.display = "block"; dots[slideIndex - 1].className += " active"; // 每两秒展示下一张图片 setTimeout(showSlides, 2000); } </script> </body> </html>