我使用JS已经有一段时间了,在创建这个模态img时遇到了问题。
我遵循这本指南。
模态脚本正在正常工作。如果我点击图像";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";
}
为了获得所需的结果,您需要修改事件函数img.onclick = function(){}
。
声明要放置默认图像(预览(的变量:
let default_image = this.src;
对于这个.src,你需要为模式窗口分配图像路径(我选择了一张随机图片(:
this.src = 'https://www.12542.ru/206-28.jpg';
为了让默认图片返回,你需要做这个
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>
modalImg.src = document.getElementById('myImgToReplace').src;
你试过这个吗?