如何使用CSS和JavaScript创建响应式幻灯片画廊?
在本文中,我们将借助JavaScript和CSS创建一个响应式幻灯片画廊。
响应式幻灯片将显示一系列带有文字的图片和两个箭头按钮,您可以使用它们浏览图片。**响应式幻灯片画廊**或**响应式图片滑块画廊**与响应式图片幻灯片相同。它是一系列您可以浏览的图片。但它不是使用箭头标记,而是提供图片缩略图,在页面底部以小视图显示幻灯片中图片的预览。
使用它,您可以更轻松地浏览图片,您可以使用缩略图从提供的图片集中选择随机图片。
以下是创建响应式幻灯片画廊的步骤。在这个例子中,我们正在创建一个显示“**响应式幻灯片画廊**”的网页。
Example.html
创建一个**HTML**文件,我们将在其中定义页面的结构(视图)。在这个例子中,我们使用HTML代码创建当前页面,其中包含所需的图片和响应式幻灯片画廊。
<body>
<h3 style="text-align: center">Slideshow Gallery</h3>
<div class="container">
<div class="imgSlides">
<div class="numbertext">1 / 6</div>
<img src="https://images.pexels.com/photos/1537979/pexels-photo-1537979.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
</div>
<div class="imgSlides">
<div class="numbertext">2 / 6</div>
<img src="https://images.pexels.com/photos/59512/pexels-photo-59512.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
</div>
<div class="imgSlides">
<div class="numbertext">3 / 6</div>
<img src="https://images.pexels.com/photos/2658459/pexels-photo-2658459.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
</div>
<div class="imgSlides">
<div class="numbertext">4 / 6</div>
<img src="https://images.pexels.com/photos/24821/pexels-photo-24821.jpg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
</div>
<div class="imgSlides">
<div class="numbertext">5 / 6</div>
<img src="https://images.pexels.com/photos/5689662/pexels-photo-5689662.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
</div>
<div class="imgSlides">
<div class="numbertext">6 / 6</div>
<img src="https://images.pexels.com/photos/9918913/pexels-photo-9918913.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
<div class="caption-container">
<p id="caption"></p>
</div>
<div class="row">
<div class="column">
<img class="demo cursor" src="https://images.pexels.com/photos/1537979/pexels-photo-1537979.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(1)" alt="One" />
</div>
<div class="column">
<img class="demo cursor" src="https://images.pexels.com/photos/59512/pexels-photo-59512.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(2)" alt="Two" />
</div>
<div class="column">
<img class="demo cursor" src="https://images.pexels.com/photos/2658459/pexels-photo-2658459.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(3)" alt="Three" />
</div>
<div class="column">
<img class="demo cursor" src="https://images.pexels.com/photos/24821/pexels-photo-24821.jpg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(4)" alt="Four" />
</div>
<div class="column">
<img class="demo cursor" src="https://images.pexels.com/photos/5689662/pexels-photo-5689662.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(5)" alt="Five" />
</div>
<div class="column">
<img class="demo cursor" src="https://images.pexels.com/photos/9918913/pexels-photo-9918913.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(6)" alt="Six" />
</div>
</div>
</div>
Example.css
添加**CSS样式**以设置大小和效果。在这个例子中,我们正在为幻灯片设置样式,并在我们悬停在图片上时添加悬停效果,使其更清晰。
<style>
body {
font-family: Arial, Helvetica, sans-serif;
margin: 0;
}
* {
box-sizing: border-box;
}
img {
vertical-align: middle;
height: 400px;
}
/* Position the image container */
.container {
position: relative;
}
/* Hide the images by default */
.imgSlides {
display: none;
}
.cursor {
cursor: pointer;
}
/* Next & previous buttons */
.prev,
.next {
cursor: pointer;
position: absolute;
top: 35%;
width: auto;
padding: 16px;
margin-top: -50px;
color: white;
font-weight: bold;
font-size: 20px;
border-radius: 0 3px 3px 0;
user-select: none;
-webkit-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);
}
/* Number text (1/3 etc) */
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
/* Container for image text */
.caption-container {
text-align: center;
background-color: #222;
padding: 2px 16px;
color: white;
}
.row:after {
content: "";
display: table;
clear: both;
}
.row img {
height: 130px;
}
/* Six columns side by side */
.column {
float: left;
width: 16.66%;
}
/* Add a transparency effect for thumnbail images */
.demo {
opacity: 0.6;
}
.active,
.demo:hover {
opacity: 1;
}
</style>
Example.js
使用**JavaScript**,我们可以执行验证并在页面上处理事件。在这个例子中,我们创建了带有两个按钮**上一个**和**下一个**的响应式幻灯片画廊。
并将图片的长度传递到JavaScript数组中,以便迭代从第一个到最后一个图片链接,或者如果我们点击了图片,它将以全视图显示。
<script>
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("imgSlides");
let dots = document.getElementsByClassName("demo");
let captionText = document.getElementById("caption");
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";
captionText.innerHTML = dots[slideIndex - 1].alt;
}
</script>
完整示例
以下是使用CSS、HTML、JavaScript和媒体查询创建响应式幻灯片画廊的完整示例。
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body {
font-family: Arial, Helvetica, sans-serif;
margin: 0;
}
* {
box-sizing: border-box;
}
img {
vertical-align: middle;
height: 400px;
}
/* Position the image container */
.container {
position: relative;
}
/* Hide the images by default */
.imgSlides {
display: none;
}
.cursor {
cursor: pointer;
}
/* Next & previous buttons */
.prev,
.next {
cursor: pointer;
position: absolute;
top: 35%;
width: auto;
padding: 16px;
margin-top: -50px;
color: white;
font-weight: bold;
font-size: 20px;
border-radius: 0 3px 3px 0;
user-select: none;
-webkit-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);
}
/* Number text (1/3 etc) */
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
/* Container for image text */
.caption-container {
text-align: center;
background-color: #222;
padding: 2px 16px;
color: white;
}
.row:after {
content: "";
display: table;
clear: both;
}
.row img {
height: 130px;
}
/* Six columns side by side */
.column {
float: left;
width: 16.66%;
}
/* Add a transparency effect for thumnbail images */
.demo {
opacity: 0.6;
}
.active,
.demo:hover {
opacity: 1;
}
</style>
<body>
<h3 style="text-align: center">Slideshow Gallery</h3>
<div class="container">
<div class="imgSlides">
<div class="numbertext">1 / 6</div>
<img src="https://images.pexels.com/photos/1537979/pexels-photo-1537979.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
</div>
<div class="imgSlides">
<div class="numbertext">2 / 6</div>
<img src="https://images.pexels.com/photos/59512/pexels-photo-59512.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
</div>
<div class="imgSlides">
<div class="numbertext">3 / 6</div>
<img src="https://images.pexels.com/photos/2658459/pexels-photo-2658459.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
</div>
<div class="imgSlides">
<div class="numbertext">4 / 6</div>
<img src="https://images.pexels.com/photos/24821/pexels-photo-24821.jpg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
</div>
<div class="imgSlides">
<div class="numbertext">5 / 6</div>
<img src="https://images.pexels.com/photos/5689662/pexels-photo-5689662.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
</div>
<div class="imgSlides">
<div class="numbertext">6 / 6</div>
<img src="https://images.pexels.com/photos/9918913/pexels-photo-9918913.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" />
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
<div class="caption-container">
<p id="caption"></p>
</div>
<div class="row">
<div class="column">
<img class="demo cursor" src="https://images.pexels.com/photos/1537979/pexels-photo-1537979.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(1)" alt="One" />
</div>
<div class="column">
<img class="demo cursor" src="https://images.pexels.com/photos/59512/pexels-photo-59512.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(2)" alt="Two" />
</div>
<div class="column">
<img class="demo cursor" src="https://images.pexels.com/photos/2658459/pexels-photo-2658459.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(3)" alt="Three" />
</div>
<div class="column">
<img class="demo cursor" src="https://images.pexels.com/photos/24821/pexels-photo-24821.jpg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(4)" alt="Four" />
</div>
<div class="column">
<img class="demo cursor" src="https://images.pexels.com/photos/5689662/pexels-photo-5689662.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(5)" alt="Five" />
</div>
<div class="column">
<img class="demo cursor" src="https://images.pexels.com/photos/9918913/pexels-photo-9918913.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width: 100%" onclick="currentSlide(6)" alt="Six" />
</div>
</div>
</div>
<script>
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("imgSlides");
let dots = document.getElementsByClassName("demo");
let captionText = document.getElementById("caption");
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";
captionText.innerHTML = dots[slideIndex - 1].alt;
}
</script>
</body>
</html>
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP