单击时替换模态图像
Replace modal image on Click
我已经有一段时间没有使用 JS 了,我在创建这个 modal-img 时遇到了问题。
我正在关注 this 指南。
模态脚本正常工作。如果我单击图像“img_snow.jpg”,它会打开具有相同图像的模态,但我真正需要的是单击图像并打开具有不同图像“img_fire.jpg”的模态,而不是我之前点击过的那个。
我试图仅在模式弹出时将“img_snow.jpg”替换为“img_fire.jpg”。
关于如何解决这个问题的任何线索?
<!-- Image to show on the modal -->
<img id="myImgToReplace" src="img_fire.jpg">
<!-- Trigger the Modal -->
<img id="myImg" src="img_snow.jpg" alt="Snow">
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- The Close Button -->
<span class="close">×</span>
<!-- Modal Content (The Image) -->
<img class="modal-content" id="img01">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>
var modal = document.getElementById("myModal");
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById("myImg");
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;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
modalImg.src = document.getElementById('myImgToReplace').src;
你试过吗?
为了得到想要的结果,需要修改事件函数img.onclick = function(){}
。
声明一个要放置默认图像(预览)的变量:
let default_image = this.src;
对于this.src你需要为模态分配图片路径window(我随机选择了一张图片):
this.src = 'https://www.12542.ru/206-28.jpg';
为了让默认图片变成return,你需要这样做
this.src = default_image;
最后,你的函数应该是这样的:
img.onclick = function(){
let default_image = this.src;
modal.style.display = "block";
this.src = 'https://www.12542.ru/206-28.jpg';
modalImg.src = this.src;
this.src = default_image;
captionText.innerHTML = this.alt;
}
var modal = document.getElementById("myModal");
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById("myImg");
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function(){
let default_image = this.src;
modal.style.display = "block";
this.src = 'https://www.12542.ru/206-28.jpg';
modalImg.src = this.src;
this.src = default_image;
captionText.innerHTML = this.alt;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
#myImg {
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
}
#myImg:hover {opacity: 0.7;}
/* The Modal (background) */
.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 */
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}
/* Add Animation */
.modal-content, #caption {
-webkit-animation-name: zoom;
-webkit-animation-duration: 0.6s;
animation-name: zoom;
animation-duration: 0.6s;
}
@-webkit-keyframes zoom {
from {-webkit-transform:scale(0)}
to {-webkit-transform:scale(1)}
}
@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%;
}
}
<img id="myImg" src="https://s1.1zoom.ru/big0/98/Slovenia_Summer_Lake_487816.jpg" alt="Snow" style="width:100%;max-width:300px">
<!-- The Modal -->
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
<div id="caption"></div>
</div>
第二个解决方案:
var modal = document.getElementById("myModal");
var img = document.querySelectorAll(".myImg");
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
const img_modal = ['https://static3.depositphotos.com/1000992/133/i/600/depositphotos_1337508-stock-photo-a-free-flying-white-dove.jpg',
'https://i.pinimg.com/474x/9c/a1/7e/9ca17e8e343e1ebfee9a463fc4ec22fe.jpg',
'https://everskate.com/wp-content/uploads/2017/12/how-to-kickflip-trick-tip-600x336.jpg'];
Array.from(img).forEach(function(imgArray, i) {
imgArray.onclick = function(){
let default_image = this.src;
modal.style.display = "block";
this.src = img_modal[i];
modalImg.src = this.src;
this.src = default_image;
captionText.innerHTML = this.alt;
}
});
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
}
.myImg {
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
}
.myImg:hover {opacity: 0.7;}
/* The Modal (background) */
.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 */
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}
/* Add Animation */
.modal-content, #caption {
-webkit-animation-name: zoom;
-webkit-animation-duration: 0.6s;
animation-name: zoom;
animation-duration: 0.6s;
}
@-webkit-keyframes zoom {
from {-webkit-transform:scale(0)}
to {-webkit-transform:scale(1)}
}
@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%;
}
}
<img class="myImg" src="https://s1.1zoom.ru/big0/98/Slovenia_Summer_Lake_487816.jpg" alt="Snow" style="width:100%;max-width:300px">
<img class="myImg" src="https://s1.1zoom.ru/big0/235/Poppies_Summer_Grasslands_Trees_562184_1270x1024.jpg" alt="Snow" style="width:100%;max-width:300px">
<img class="myImg" src="https://akiwa.ru/upload/dev2fun_opengraph/ccf/ccf8acce0f0bdabc13d15f4f1ff6c549.jpg" alt="Snow" style="width:100%;max-width:300px">
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
<div id="caption"></div>
</div>
我已经有一段时间没有使用 JS 了,我在创建这个 modal-img 时遇到了问题。
我正在关注 this 指南。
模态脚本正常工作。如果我单击图像“img_snow.jpg”,它会打开具有相同图像的模态,但我真正需要的是单击图像并打开具有不同图像“img_fire.jpg”的模态,而不是我之前点击过的那个。
我试图仅在模式弹出时将“img_snow.jpg”替换为“img_fire.jpg”。
关于如何解决这个问题的任何线索?
<!-- Image to show on the modal -->
<img id="myImgToReplace" src="img_fire.jpg">
<!-- Trigger the Modal -->
<img id="myImg" src="img_snow.jpg" alt="Snow">
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- The Close Button -->
<span class="close">×</span>
<!-- Modal Content (The Image) -->
<img class="modal-content" id="img01">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>
var modal = document.getElementById("myModal");
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById("myImg");
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;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
modalImg.src = document.getElementById('myImgToReplace').src;
你试过吗?
为了得到想要的结果,需要修改事件函数img.onclick = function(){}
。
声明一个要放置默认图像(预览)的变量:
let default_image = this.src;
对于this.src你需要为模态分配图片路径window(我随机选择了一张图片):
this.src = 'https://www.12542.ru/206-28.jpg';
为了让默认图片变成return,你需要这样做
this.src = default_image;
最后,你的函数应该是这样的:
img.onclick = function(){
let default_image = this.src;
modal.style.display = "block";
this.src = 'https://www.12542.ru/206-28.jpg';
modalImg.src = this.src;
this.src = default_image;
captionText.innerHTML = this.alt;
}
var modal = document.getElementById("myModal");
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById("myImg");
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function(){
let default_image = this.src;
modal.style.display = "block";
this.src = 'https://www.12542.ru/206-28.jpg';
modalImg.src = this.src;
this.src = default_image;
captionText.innerHTML = this.alt;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
#myImg {
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
}
#myImg:hover {opacity: 0.7;}
/* The Modal (background) */
.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 */
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}
/* Add Animation */
.modal-content, #caption {
-webkit-animation-name: zoom;
-webkit-animation-duration: 0.6s;
animation-name: zoom;
animation-duration: 0.6s;
}
@-webkit-keyframes zoom {
from {-webkit-transform:scale(0)}
to {-webkit-transform:scale(1)}
}
@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%;
}
}
<img id="myImg" src="https://s1.1zoom.ru/big0/98/Slovenia_Summer_Lake_487816.jpg" alt="Snow" style="width:100%;max-width:300px">
<!-- The Modal -->
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
<div id="caption"></div>
</div>
第二个解决方案:
var modal = document.getElementById("myModal");
var img = document.querySelectorAll(".myImg");
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
const img_modal = ['https://static3.depositphotos.com/1000992/133/i/600/depositphotos_1337508-stock-photo-a-free-flying-white-dove.jpg',
'https://i.pinimg.com/474x/9c/a1/7e/9ca17e8e343e1ebfee9a463fc4ec22fe.jpg',
'https://everskate.com/wp-content/uploads/2017/12/how-to-kickflip-trick-tip-600x336.jpg'];
Array.from(img).forEach(function(imgArray, i) {
imgArray.onclick = function(){
let default_image = this.src;
modal.style.display = "block";
this.src = img_modal[i];
modalImg.src = this.src;
this.src = default_image;
captionText.innerHTML = this.alt;
}
});
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
}
.myImg {
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
}
.myImg:hover {opacity: 0.7;}
/* The Modal (background) */
.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 */
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}
/* Add Animation */
.modal-content, #caption {
-webkit-animation-name: zoom;
-webkit-animation-duration: 0.6s;
animation-name: zoom;
animation-duration: 0.6s;
}
@-webkit-keyframes zoom {
from {-webkit-transform:scale(0)}
to {-webkit-transform:scale(1)}
}
@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%;
}
}
<img class="myImg" src="https://s1.1zoom.ru/big0/98/Slovenia_Summer_Lake_487816.jpg" alt="Snow" style="width:100%;max-width:300px">
<img class="myImg" src="https://s1.1zoom.ru/big0/235/Poppies_Summer_Grasslands_Trees_562184_1270x1024.jpg" alt="Snow" style="width:100%;max-width:300px">
<img class="myImg" src="https://akiwa.ru/upload/dev2fun_opengraph/ccf/ccf8acce0f0bdabc13d15f4f1ff6c549.jpg" alt="Snow" style="width:100%;max-width:300px">
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
<div id="caption"></div>
</div>