[英]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.