如何使用 CSS 和 JavaScript 创建响应式幻灯片?
在本文中,我们将借助JavaScript和CSS创建一个响应式幻灯片。
响应式幻灯片,或者我们可以称之为响应式图片滑块,或者带有文字的响应式图片滑块,是一种视觉上具有吸引力和交互性的设计元素,它会让你的网站访客参与进来,它将包含一个或多个带有文字叠加的图片。
也就是说,它会显示一系列带有文字的图片。它还提供两个箭头按钮,您可以使用它们浏览图片(双向)。
这用于在网页上呈现一系列图片,占用较少的空间,并且这些图片易于访问。
在本例中,我们正在创建一个显示“响应式幻灯片”的网页。
Example.html
创建一个HTML文件,我们将在其中定义页面的结构(视图)。在本例中,使用 HTML 代码,我们正在创建当前页面,其中包含所需的图片,一个响应式图片幻灯片。
<body> <!--HTML--> <div class="slideshow-container"> <div class="mySlides fade"> <div class="numbertext">1 / 3</div> <img src="https://images.pexels.com/photos/57652/pexels-photo-57652.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" /> <div class="text">One</div> </div> <div class="mySlides fade"> <div class="numbertext">2 / 3</div> <img src="https://images.pexels.com/photos/3540375/pexels-photo-3540375.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" /> <div class="text">Two</div> </div> <div class="mySlides fade"> <div class="numbertext">3 / 3</div> <img src="https://images.pexels.com/photos/811029/pexels-photo-811029.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" /> <div class="text">Three</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>
Example.css
添加CSS 样式以提供大小和效果以获得外观。在本例中,我们正在为幻灯片设置样式。
<style> /* CSS */ * { box-sizing: border-box; } body { font-family: Verdana, sans-serif; margin: 0; } .mySlides { display: none; } img { vertical-align: middle; width: 100%; height: 400px; } /* Slideshow container */ .slideshow-container { max-width: 100vw; position: relative; margin-top: 5%; } /* Next & previous buttons */ .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; user-select: none; } /* Position the "next button" to the right */ .next { right: 0; border-radius: 3px 0 0 3px; } /* On hover, add a black background color with a little bit see-through */ .prev:hover, .next:hover { background-color: rgba(0, 0, 0, 0.8); } /* Caption text */ .text { color: #ff0000; font-size: 15px; padding: 8px 12px; position: absolute; bottom: 8px; width: 100%; text-align: center; } /* Number text (1/3 etc) */ .numbertext { color: #f2f2f2; font-size: 12px; padding: 8px 12px; position: absolute; top: 0; } /* The dots/bullets/indicators */ .dot { cursor: pointer; height: 15px; width: 15px; margin: 0 2px; background-color: #bbb; border-radius: 50%; display: inline-block; transition: background-color 0.6s ease; } .active, .dot:hover { background-color: #46dc5a; } /* Fading animation */ .fade { animation-name: fade; animation-duration: 1.5s; } @keyframes fade { from { opacity: 0.4; } to { opacity: 1; } } /* On smaller screens, decrease text size */ @media only screen and (max-width: 300px) { .prev, .next, .text { font-size: 11px; } } </style>
Example.js
在本例中,使用两个按钮 prev 和 next 创建幻灯片。并将图片的长度传递到 JavaScript 数组中以迭代从第一个到最后一个的图片,或者我们可以通过单击按钮进行前后切换。
<script> /* javascript Code */ let slideIndex = 1; //set index at images first showSlides(slideIndex); function plusSlides(n) { showSlides((slideIndex += n)); } function currentSlide(n) { showSlides((slideIndex = n)); } function showSlides(n) { let i; let slides = document.getElementsByClassName("mySlides"); let 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>
完整示例
以下是如何借助CSS、HTML、Javascript和媒体查询创建响应式幻灯片的示例。
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style> /* CSS */ * { box-sizing: border-box; } body { font-family: Verdana, sans-serif; margin: 0; } .mySlides { display: none; } img { vertical-align: middle; width: 100%; height: 400px; } /* Slideshow container */ .slideshow-container { max-width: 100vw; position: relative; margin-top: 5%; } /* Next & previous buttons */ .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; user-select: none; } /* Position the "next button" to the right */ .next { right: 0; border-radius: 3px 0 0 3px; } /* On hover, add a black background color with a little bit see-through */ .prev:hover, .next:hover { background-color: rgba(0, 0, 0, 0.8); } /* Caption text */ .text { color: #ff0000; font-size: 15px; padding: 8px 12px; position: absolute; bottom: 8px; width: 100%; text-align: center; } /* Number text (1/3 etc) */ .numbertext { color: #f2f2f2; font-size: 12px; padding: 8px 12px; position: absolute; top: 0; } /* The dots/bullets/indicators */ .dot { cursor: pointer; height: 15px; width: 15px; margin: 0 2px; background-color: #bbb; border-radius: 50%; display: inline-block; transition: background-color 0.6s ease; } .active, .dot:hover { background-color: #46dc5a; } /* Fading animation */ .fade { animation-name: fade; animation-duration: 1.5s; } @keyframes fade { from { opacity: 0.4; } to { opacity: 1; } } /* On smaller screens, decrease text size */ @media only screen and (max-width: 300px) { .prev, .next, .text { font-size: 11px; } } </style> </head> <body> <!--HTML--> <div class="slideshow-container"> <div class="mySlides fade"> <div class="numbertext">1 / 3</div> <img src="https://images.pexels.com/photos/57652/pexels-photo-57652.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" /> <div class="text">One</div> </div> <div class="mySlides fade"> <div class="numbertext">2 / 3</div> <img src="https://images.pexels.com/photos/3540375/pexels-photo-3540375.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" /> <div class="text">Two</div> </div> <div class="mySlides fade"> <div class="numbertext">3 / 3</div> <img src="https://images.pexels.com/photos/811029/pexels-photo-811029.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" /> <div class="text">Three</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> /* javascript Code */ let slideIndex = 1; showSlides(slideIndex); function plusSlides(n) { showSlides((slideIndex += n)); } function currentSlide(n) { showSlides((slideIndex = n)); } function showSlides(n) { let i; let slides = document.getElementsByClassName("mySlides"); let 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>
广告