[英]Javascript responsive image gallery thumbnail
我需要做一个响应式图像缩略图库,我的当前看起来像这样,您可以在代码段中看到它。
由于必须显示30张图像,是否有一种方法可以使我的代码更高效,从而不必一直重复图像?
不幸的是,我无法使用插件,但可以自由使用jQuery。
我的HTML和CSS看起来像这样:
HTML:
<h2 style="text-align:center">Lightbox</h2>
<div class="row">
<div class="column">
<img src="https://placehold.it/180x180.jpg " style="width:100%" onclick="openModal();currentSlide(1)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="https://placehold.it/180x180.jpg " style="width:100%" onclick="openModal();currentSlide(2)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="https://placehold.it/180x180.jpg " style="width:100%" onclick="openModal();currentSlide(3)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="https://placehold.it/180x180.jpg " style="width:100%" onclick="openModal();currentSlide(4)" class="hover-shadow cursor">
</div>
</div>
<div id="myModal" class="modal">
<span class="close cursor" onclick="closeModal()">×</span>
<div class="modal-content">
<div class="mySlides">
<div class="numbertext">1 / 4</div>
<img src="https://placehold.it/180x180.jpg " style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">2 / 4</div>
<img src="https://placehold.it/180x180.jpg " style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">3 / 4</div>
<img src="https://placehold.it/180x180.jpg " style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">4 / 4</div>
<img src="https://placehold.it/180x180.jpg " 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="column">
<img class="demo cursor" src="https://placehold.it/180x180.jpg " style="width:100%" onclick="currentSlide(1)" alt="Nature and sunrise">
</div>
<div class="column">
<img class="demo cursor" src="https://placehold.it/180x180.jpg " style="width:100%" onclick="currentSlide(2)" alt="Trolltunga, Norway">
</div>
<div class="column">
<img class="demo cursor" src="https://placehold.it/180x180.jpg " style="width:100%" onclick="currentSlide(3)" alt="Mountains and fjords">
</div>
<div class="column">
<img class="demo cursor" src="https://placehold.it/180x180.jpg " style="width:100%" onclick="currentSlide(4)" alt="Northern Lights">
</div>
</div>
</div>
CSS:
body {
font-family: Verdana, sans-serif;
margin: 0;
}
* {
box-sizing: border-box;
}
.row > .column {
padding: 0 8px;
}
.row:after {
content: "";
display: table;
clear: both;
}
.column {
float: left;
width: 25%;
}
/* The Modal (background) */
.modal {
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: black;
}
/* Modal Content */
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
width: 90%;
max-width: 1200px;
}
/* The Close Button */
.close {
color: white;
position: absolute;
top: 10px;
right: 25px;
font-size: 35px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #999;
text-decoration: none;
cursor: pointer;
}
.mySlides {
display: none;
}
.cursor {
cursor: pointer
}
/* Next & previous buttons */
.prev,
.next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -50px;
color: white;
font-weight: bold;
font-size: 20px;
transition: 0.6s ease;
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;
}
img {
margin-bottom: -4px;
}
.caption-container {
text-align: center;
background-color: black;
padding: 2px 16px;
color: white;
}
.demo {
opacity: 0.6;
}
.active,
.demo:hover {
opacity: 1;
}
img.hover-shadow {
transition: 0.3s
}
.hover-shadow:hover {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19)
}
您将不得不以一种或另一种方式使用图像元素,因此这不应成为您的重点。 但是,当然您还可以采取其他措施来减少冗余,分离内容,表示形式和行为,并为自己提供更好的方式来随着时间的流逝来扩展和调试页面。
首先,不要将内联样式应用于元素,而应使用CSS选择器来减少冗余并将内容与表示分离。
另外,虽然静态HTML不会使用JavaScript运行时的任何功能,但是您可以自动化这些类的应用程序以及设置click事件处理程序(无论如何都不应使用内联HTML事件处理属性来完成)使用JavaScript。 这不会消除所有图像元素,但是会减少该代码的冗余。
其他几件事...您的某些HTML无效,因为您的某些图像元素没有必需的alt
属性,无法为使用屏幕阅读器软件或看不到图像的用户提供替代文本描述。
最后,看起来您的许多图像路径在属性值中都有一个尾随空格。
// Place all of this code into a <script></script> element and place that element // just before the closing of the <body> element (</body>) so that by the time // the script is reached, all the HTML will have already been parsed into memory. // Get references to the elements you need to work with: var prev = document.querySelector(".prev"); var next = document.querySelector(".next"); var close = document.querySelector(".close"); // Get a node list of the sets of images and turn the node list into a proper array var columnImages1 = Array.prototype.slice.call(document.querySelectorAll("div.row > div.colum > img.hover-shadow.cursor")); var columnImages2 = Array.prototype.slice.call(document.querySelectorAll("div.column > img.demo")); // Loop over the first group of images and set up their click event handlers columnImages1.forEach(function(img, index){ img.addEventListener("click", function(){ openModal(); currentSlide(index + 1); }); }); // Loop over the second group of images and set up their click event handlers columnImages1.forEach(function(img, index){ img.addEventListener("click", function(){ currentSlide(index + 1); }); }); // Set up the click event handler for the close, previous and next buttons close.addEventListener("click", closeModal); prev.addEventListener("click", function(){ plusSlides(-1) }); next.addEventListener("click", function(){ plusSlides(1) });
* { box-sizing: border-box; } body { font-family: Verdana, sans-serif; margin: 0; } /* This will eliminate the need to repeat "width:100%" on all your slides */ .mySlides > img, .column > img { width: 100%; } .row > .column { padding: 0 8px; } .row:after { content: ""; display: table; clear: both; } .column { float: left; width: 25%; } /* The Modal (background) */ .modal { display: none; position: fixed; z-index: 1; padding-top: 100px; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: black; }
<h2 style="text-align:center">Lightbox</h2> <div class="row"> <div class="column"> <img src="https://placehold.it/180x180.jpg" alt="You need a short description here" class="hover-shadow cursor"> </div> <div class="column"> <img src="https://placehold.it/180x180.jpg" alt="You need a short description here" class="hover-shadow cursor"> </div> <div class="column"> <img src="https://placehold.it/180x180.jpg" alt="You need a short description here" class="hover-shadow cursor"> </div> <div class="column"> <img src="https://placehold.it/180x180.jpg" alt="You need a short description here" class="hover-shadow cursor"> </div> </div> <div id="myModal" class="modal"> <span class="close cursor">×</span> <div class="modal-content"> <div class="mySlides"> <div class="numbertext">1 / 4</div> <img src="https://placehold.it/180x180.jpg" alt="You need a short description here"> </div> <div class="mySlides"> <div class="numbertext">2 / 4</div> <img src="https://placehold.it/180x180.jpg" alt="You need a short description here"> </div> <div class="mySlides"> <div class="numbertext">3 / 4</div> <img src="https://placehold.it/180x180.jpg" alt="You need a short description here"> </div> <div class="mySlides"> <div class="numbertext">4 / 4</div> <img src="https://placehold.it/180x180.jpg" alt="You need a short description here"> </div> <a class="prev">❮</a> <a class="next">❯</a> <div class="caption-container"> <p id="caption"></p> </div> <div class="column"> <img class="demo cursor" src="https://placehold.it/180x180.jpg" alt="Nature and sunrise"> </div> <div class="column"> <img class="demo cursor" src="https://placehold.it/180x180.jpg" alt="Trolltunga, Norway"> </div> <div class="column"> <img class="demo cursor" src="https://placehold.it/180x180.jpg" alt="Mountains and fjords"> </div> <div class="column"> <img class="demo cursor" src="https://placehold.it/180x180.jpg" alt="Northern Lights"> </div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.