Javascript响应图像库缩略图
我需要做一个快速响应的图像缩略图库,我的现在看起来就像你在片段中看到的那样 既然我必须有30个图像显示,有没有一种方法可以使我的代码更有效,这样我就不必一直重复图像 不幸的是,我不能使用插件,但我可以自由使用jQuery 我的html和css如下所示: HTML:Javascript响应图像库缩略图,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我需要做一个快速响应的图像缩略图库,我的现在看起来就像你在片段中看到的那样 既然我必须有30个图像显示,有没有一种方法可以使我的代码更有效,这样我就不必一直重复图像 不幸的是,我不能使用插件,但我可以自由使用jQuery 我的html和css如下所示: HTML: <h2 style="text-align:center">Lightbox</h2> <div class="row"> <div class="column"> &l
<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选择器减少冗余并将内容与表示分离 此外,虽然静态HTML不使用JavaScript运行时的任何功能,但您可以使用JavaScript自动化这些类的应用程序以及单击事件处理程序的设置(无论如何不应该使用内联HTML事件处理属性完成)。这不会消除所有图像元素,但会减少代码的冗余 还有几件事。。。某些HTML无效,因为某些图像元素没有所需的
alt
属性,无法为使用屏幕阅读器软件或无法查看图像的用户提供备用文本描述
最后,看起来许多图像路径都有一个尾随空间作为属性值的一部分
//将所有这些代码放入一个元素中,然后放入该元素
//在关闭元素()之前,以便
//到达脚本后,所有HTML都已解析到内存中。
//获取对需要使用的元素的引用:
var prev=document.querySelector(“.prev”);
var next=document.querySelector(“.next”);
var close=document.querySelector(“.close”);
//获取图像集的节点列表,并将节点列表转换为适当的数组
var columnImages1=Array.prototype.slice.call(document.queryselectoral(“div.row>div.column>img.hover shadow.cursor”);
var columnImages2=Array.prototype.slice.call(document.queryselectoral(“div.column>img.demo”);
//循环第一组图像并设置其单击事件处理程序
columnImages1.forEach(函数(img,索引){
img.addEventListener(“单击”,函数(){
openModal();
当前幻灯片(索引+1);
});
});
//循环第二组图像并设置其单击事件处理程序
columnImages1.forEach(函数(img,索引){
img.addEventListener(“单击”,函数(){
当前幻灯片(索引+1);
});
});
//为“关闭”、“上一步”和“下一步”按钮设置单击事件处理程序
close.addEventListener(“单击”,closeModal);
prev.addEventListener(“单击”,function(){plusSlides(-1)});
addEventListener(“单击”,function(){plusSlides(1)})代码>
*{框大小:边框框;}
身体{
字体系列:Verdana,无衬线;
保证金:0;
}
/*这将消除在所有幻灯片上重复“宽度:100%”的需要*/
.mySlides>img,.column>img{宽度:100%;}
.row>列{
填充:0 8px;
}
.罗:之后{
内容:“;
显示:表格;
明确:两者皆有;
}
.栏目{
浮动:左;
宽度:25%;
}
/*模态(背景)*/
.莫代尔{
显示:无;
位置:固定;
z指数:1;
填充顶部:100px;
左:0;
排名:0;
宽度:100%;
身高:100%;
溢出:自动;
背景色:黑色;
}
灯箱
&时代;
1/4
2/4
3/4
4/4
❮
❯
您的问题不清楚。您希望在哪些方面实现优化?您尝试了哪些方法来减少所需的标记量?您是否尝试过任何工具或框架来帮助您?您可以使用哪些工具或框架来帮助您解决问题?我知道你在问什么,但是如果没有一个参照系,要恰当地回答你就有点困难。有人告诉我不要使用任何框架,这让我很难按照我的方式来做,我基本上必须将每张图片重复3次。如果继续问下去,可能会更好,但是请先阅读他们的文章。我有点理解你的意思。但是我知道你的代码片段目前没有运行。希望你能理解,我知道我的代码片段没有运行。这是因为它引用了在单击时调用的函数,而您没有提供(openmodel
,currentSlide
)。我的回答显示了如何重构代码以使其更加优化,但由于您没有提供所有代码,我只能告诉您这么多。我的javascript目前并没有真正超越DOM的基础,所以我现在真的对如何做感到困惑。@WosleyAlarico,我认为优化代码目前不应该成为一个问题。
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)
}