gpt4 book ai didi

javascript - 如何制作模态图库?无需查询

转载 作者:行者123 更新时间:2023-12-01 00:17:08 24 4
gpt4 key购买 nike

var modal = document.getElementById("myModal");

var img = document.getElementsByClassName("img");
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");

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";
}
.row{
display: flex;
flex-wrap: wrap;
padding: 0 4px;
justify-content: center;
}

.column{
flex: 1 0 22%;
max-width: 25%;
padding: 0 4px;
}

.column img{
margin-top: 8px;
vertical-align: middle;
width: 100%;
box-shadow: 3px 3px 2px grey;
border-radius: 8px;
}


.img {
cursor: pointer;
transition: 0.3s;
}

.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.9); /* Black w/ opacity */
}

/* Modal Content (Image) */
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}

/* Caption of Modal Image (Image Text) - Same Width as the Image */
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}

/* Add Animation - Zoom in the Modal */
.modal-content, #caption {
animation-name: zoom;
animation-duration: 0.6s;
}

@keyframes zoom {
from {transform:scale(0)}
to {transform:scale(1)}
}

/* The Close Button */
.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;
}

/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px){
.modal-content {
width: 100%;
}
}
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<div class="conta">
<div class="row">
<div class="column">
<img class="img" alt="example" src="example.jpg">
<img class="img" alt="example" src="example/example.jpg">
</div>
<div class="column">
<img class="img" alt="example." src="example/example.jpg">
<img class="img" alt="example"src="example/example.jpg">
<img class="img" alt="example" src="example/example.jpg">
</div>
<div class="column">
<img class="img" alt="example" src="example/example.jpg">
<img class="img" alt="example" src="example/example.jpg">
<img class="img" alt="example" src="example/example.jpg">
</div>
<div class="column">
<img class="img" alt="example" src="example/example.jpg">
<img class="img" alt="example" src="example/example.jpg">
<img class="img" alt="example" src="example/example.jpg">
</div>
</div>
</div>
<div id="myModal" class="modal">
<span class="close">&times;</span>
<img class="modal-content" id="img01">
<div id="caption"></div>
</div>
<script src="lightBox.js"></script>
</body>
</html>

我正在尝试制作一个模态图像库。我尝试用 img 类制作所有图像。但没有任何反应,我无法单击图像。我的检查器出现错误。这是“无法设置未定义的属性‘onclick’”。在 span.onclick = function() { 行上,我尝试了下面的两种链接方法。两者都不适合我。

Modal image galleries - multiple images
https://www.w3schools.com/howto/howto_css_modal_images.asp

这只能通过查询实现吗?我真的不想用它。

最佳答案

为了帮助您,您只需迭代 for loopdocument.getElementsByClassName ("img") 返回的项目即可。 并将监听器分配给每个人。

这里我放了片段来说明上面的内容:

var modal = document.getElementById("myModal");

var imgs = document.getElementsByClassName("img");
for(let img of imgs){
img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
}
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");

document.getElementsByClassName("close")[0].addEventListener('click',function() {
modal.style.display = "none";
})
.row{
display: flex;
flex-wrap: wrap;
padding: 0 4px;
justify-content: center;
}

.column{
flex: 1 0 22%;
max-width: 25%;
padding: 0 4px;
}

.column img{
margin-top: 8px;
vertical-align: middle;
width: 100%;
box-shadow: 3px 3px 2px grey;
border-radius: 8px;
}


.img {
cursor: pointer;
transition: 0.3s;
}

.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.9); /* Black w/ opacity */
}

/* Modal Content (Image) */
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}

/* Caption of Modal Image (Image Text) - Same Width as the Image */
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}

/* Add Animation - Zoom in the Modal */
.modal-content, #caption {
animation-name: zoom;
animation-duration: 0.6s;
}

@keyframes zoom {
from {transform:scale(0)}
to {transform:scale(1)}
}

/* The Close Button */
.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;
}

/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px){
.modal-content {
width: 100%;
}
}
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<div class="conta">
<div class="row">
<div class="column">
<img class="img" alt="example" src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Bee_balm_seedhead_%2870424%29.jpg/1024px-Bee_balm_seedhead_%2870424%29.jpg">
<img class="img" alt="example" src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Bee_balm_seedhead_%2870424%29.jpg/1024px-Bee_balm_seedhead_%2870424%29.jpg">
</div>
<div class="column">
<img class="img" alt="example." src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Bee_balm_seedhead_%2870424%29.jpg/1024px-Bee_balm_seedhead_%2870424%29.jpg">
<img class="img" alt="example"src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Bee_balm_seedhead_%2870424%29.jpg/1024px-Bee_balm_seedhead_%2870424%29.jpg">
<img class="img" alt="example" src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Bee_balm_seedhead_%2870424%29.jpg/1024px-Bee_balm_seedhead_%2870424%29.jpg">
</div>
<div class="column">
<img class="img" alt="example" src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Bee_balm_seedhead_%2870424%29.jpg/1024px-Bee_balm_seedhead_%2870424%29.jpg">
<img class="img" alt="example" src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Bee_balm_seedhead_%2870424%29.jpg/1024px-Bee_balm_seedhead_%2870424%29.jpg">
<img class="img" alt="example" src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Bee_balm_seedhead_%2870424%29.jpg/1024px-Bee_balm_seedhead_%2870424%29.jpg">
</div>
<div class="column">
<img class="img" alt="example" src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Bee_balm_seedhead_%2870424%29.jpg/1024px-Bee_balm_seedhead_%2870424%29.jpg">
<img class="img" alt="example" src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Bee_balm_seedhead_%2870424%29.jpg/1024px-Bee_balm_seedhead_%2870424%29.jpg">
<img class="img" alt="example" src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Bee_balm_seedhead_%2870424%29.jpg/1024px-Bee_balm_seedhead_%2870424%29.jpg">
</div>
</div>
</div>
<div id="myModal" class="modal">
<span class="close">&times;</span>
<img class="modal-content" id="img01">
<div id="caption"></div>
</div>
<script src="lightBox.js"></script>
</body>
</html>

关于javascript - 如何制作模态图库?无需查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59691689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com