如何使用JS进行单击扩展图像?
我使用以下方法。可悲的是,我无法工作。
关于此扩展选项或替代方案,任何建议都会非常有帮助。请注意,我是JS的新手。
提前致谢!
var modal = document.getElementById('myModal');
var img = document.getElementById('myImg');
var modalImg = document.getElementById("image");
img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
}
/*main div*/
.ponudba {
background-color: rgb(0, 0, 0);
z-index: 3;
}
.image-grid {
padding: 12px;
}
.image-row {
display: flex;
}
.image-row .image {
margin: 12px;
height: 220px;
}
.image {
background-repeat: no-repeat;
background-size: cover;
background-position: center center;
border-radius: 3px;
transition-duration: 0.5s;
filter: contrast(75%);
transition: all 0.3s ease-in;
box-shadow: 0 3px 4px rgba(0, 0, 0, 0.3),
0 3px 4px rgba(0, 0, 0, 0.15),
0 3px 4px rgba(0, 0, 0, 0.7);
margin: 0 0 0 15%;
display: block;
height: 100vh;
max-width: 100%;
animation-name: zoom;
animation-duration: 0.6s;
z-index: 3;
}
.image:hover {
filter: contrast(100%);
transition: all 0.3s ease-out;
cursor: pointer;
}
.image-01 {
background-image: url(images/...jpg);
flex: 2;
background-position: 50% 60%;
}
.image-02 {
background-image: url(images/...jpg);
flex: 1.2;
}
.image-03 {
background-image: url(images/...jpg);
flex: 1.5;
background-position: 50% 70%;
}
.image-04 {
background-image: url(images/...jpg);
flex: 3;
background-position: 50% 60%;
}
.image-05 {
background-image: url(images/...jpg);
flex: 3;
}
.image-06 {
background-image: url(images/...jpg);
flex: 2;
}
.image-07 {
background-image: url(images/...jpg);
flex: 1.5;
}
.image-08 {
background-image: url(images/...jpg);
flex: 2.5;
background-position: 50% 70%;
}
.image-09 {
background-image: url(images/...jpg);
flex: 1;
}
.image-10 {
background-image: url(images/...jpg);
flex: 3;
background-position: 50% 80%;
}
#myImg {
border-radius: 3px;
cursor: pointer;
transition: 0.3s;
}
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.9);
}
@keyframes zoom {
from {transform: scale(0.1)}
to {transform: scale(1)}
}
.close {
position: absolute;
top: 15px;
right: 35px;
color: #f1f1f1;
font-size: 40px;
font-weight: bold;
transition: 0.3s;
}
.close:hover,
.close:focus {
color: #bbb;
text-decoration: none;
cursor: pointer;
}
@media only screen and (max-width: 700px){
.modal-content {
width: 100%;
}
.image-row {
flex-direction: column;
}
.image-row .image {
flex-basis: auto;
}
}
<div class="ponudba" id="ponudba">
<div class="image-grid">
<div class="image-row">
<div class="image image-01" id="image-01"></div>
<div class="image image-02" id="image-02"></div>
<div class="image image-03" id="image-03"></div>
<div class="image image-04" id="image-04"></div>
</div>
<div class="image-row">
<div class="image image-06" id="image-06"></div>
<div class="image image-05" id="image-05"></div>
<div class="image image-07" id="image-07"></div>
</div>
<div class="image-row">
<div class="image image-08" id="image-08"></div>
<div class="image image-09" id="image-09"></div>
<div class="image image-10" id="image-10"></div>
</div>
</div>
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="image">
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ID
应该是唯一的。如果您有多个图像,则可以使用
getElementsByClassName
来获取所需的所有元素。然后使用循环将ONCLICK函数添加到每个元素。
因为您的图像是
background -img
在CSS中,请使用backgroundImage.slice(4,-1).replace(/['“”]/g,“”)
获得 。URL
让imgs = document.getElementsbyclassName(“ image”)
for(让i = 0; i&lt; imgblocks.length; i ++)
应该是for(让i = 0; i&lt; imgs.length; i ++)
modal
未定义背景-IMG:url('...');
应该在HTML文件中写入,或者JS无法获取URL。&lt; div class =“ Image Image-01” ID =“ Image-01” style =“ backick-image:url('22 .png');”&gt;&lt;&lt;/div&gt;
”您的问题
在这种情况下,不必在每个图像(模态除外)上添加ID。
i按类名选择图像
图像
in:我是否需要为每个元素添加for loop?
您需要将
onclick
添加到您要展开的每个元素。由于我选择了class名称的图像,该对象将是类似的
用于循环添加
onclick
for every&lt; div class =“图像图像
.slice(4,-1).replace(/['“]/g,“”)
将从url(“ 22.png”)创建一个字符串,
,选择“ 22.png”
,然后替换'或“如果存在)。最终结果将为
22.png
。id
should be unique.If you have multiple images, you can use
getElementsByClassName
to get all elements you need to expand.Then use for loop to add onClick function to each element.
Because your image is
background-img
in css, usebackgroundImage.slice(4, -1).replace(/['"]/g, "")
to get url./-----Edit-----/
Sorry I made some mistakes in my code.
let imgs = document.getElementsByClassName("image-row")
should belet imgs = document.getElementsByClassName("image")
for (let i = 0; i < imgBlocks.length; i++)
should befor (let i = 0; i < imgs.length; i++)
modal
is not definedbackground-img: url('...');
should write in html file, or js can't get the url.<div class="image image-01" id="image-01" style="background-image: url('22.png');"></div>
About your question
It's not necessary to add an id on every image (except modal) in this case.
I select images by class name
image
in:Do I need to add a for loop for every element?
You need to add
onclick
to every element you want to expand.Due to I selected images by class name, the object will be something like this
Use for loop to add
onclick
for every<div class="image imag...</div>
style.backgroundImage
=url('22.png')
.slice(4, -1).replace(/['"]/g, "")
will create a string fromurl("22.png")
, select"22.png"
, then replace ' or " if exist.The final result will be
22.png
.因此,在向同事寻求帮助之后,这就是我得到的 - 带有可点击图像的图像库。
这是笔:
So after asking a collegue for help, this is what I got - an image gallery with clickable images.
Here's the pen: https://codepen.io/fullstackgenerator/pen/YzaqYdb