我正在尝试为我的网页创建一个照片页面,单击图像后,我想打开一个模式以全屏查看图像(更多或更少)。我发现本教程使用模态放大图像-https://www.w3schools.com/howto/howto_css_modal_images.asp
但是,由于页面上有多张图片并且有一个模态,因此我不确定是否有一种简单的方法可以在单击时放大图片。
是为每个图像创建单独的ID的唯一方法,还是JS中有一个选择器来一次选择多个图像?
这是JSfiddle-https://jsfiddle.net/definaly/89roukqd/2/
// Get the modal
var modal = document.getElementById("myModal");
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = $(".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";
}
/* Style the Image Used to Trigger the Modal */
.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 (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%;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Trigger the Modal -->
<img class="myImg" src="https://picsum.photos/300/300?random=1" alt="Snow" style="width:100%;max-width:300px">
<img class="myImg" src="https://picsum.photos/300/300?random=2" alt="Snow" style="width:100%;max-width:300px">
<img class="myImg" src="https://picsum.photos/300/300?random=3" alt="Snow" style="width:100%;max-width:300px">
<!-- 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>
预先感谢
答案 0 :(得分:1)
我做了很多更改,以利用jQuery和抽象出ID。随意询问其中的任何一个。
// Get the modal
var modal = $("#myModal");
var modalImg = modal.find('.modal-content');
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = $(".myImg");
var captionBox =$("#caption");
img.click(function() {
modalImg.attr('src', $(this).attr('src'));
captionBox.text( $(this).attr('alt') );
modal.show();
});
// Get the elements that closes the modal
var modalCloser = $(".close");
// When the user clicks on the close element, close the modal
modalCloser.click(function() {
modal.hide();
});
一些一般建议:
使用语义变量名称。如果您称某事为“ span”并更改标记中的元素类型,则现在会出现不匹配的情况。而是使用描述其功能的名称。
在打开器或关闭器等上设置处理程序时,请使用可捕获多个元素的jQuery类选择器。这样,您可以从多个地方打开和关闭。
了解所有jQuery的常用方法。它们可以为您节省很多打字时间,而且我发现它们非常直观,以至于记住它们只需花费很少的精力。
对于JS使用单引号,对于HTML使用双引号。这样就消除了将一个嵌套到另一个嵌套时逃脱它们的大部分需要。 jQuery文档对所有内容都使用double,但是不要紧。
请考虑迁移到let
和const
而不是var
。范围更精确,现代IDE不会一直建议您这样做。
答案 1 :(得分:1)
<img class="myImg" src="https://picsum.photos/300/300?random=1" alt="Snow" onclick="image(event)" style="width:100%;max-width:300px" />
<img class="myImg" src="https://picsum.photos/300/300?random=2" alt="Snow" onclick="image(event)" style="width:100%;max-width:300px" />
<img class="myImg" src="https://picsum.photos/300/300?random=3" alt="Snow" onclick="image(event)" style="width:100%;max-width:300px" />
// Get the modal
var modal = document.getElementById("myModal");
// Get the image and insert it inside the modal - use its "alt" text as a caption
// var img = $(".myImg");
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
function image(event) {
modal.style.display = "block";
modalImg.src = event.target.src;
captionText.innerHTML = event.target.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";
}