直接上效果
逻辑
html部分,一个img标签(一个src,一个data-img放大图片的url),一个用来装放大图片的展示框元素;
<img src="https://www.mychway.com/image/catalog/Blog/999/6.6-5.png" alt="egg" width="60" height="60" class="egg_btn" style="max-width: 200px;width: 100%;height: auto;" onclick="imgModelClick(this)"
data-img="https://www.mychway.com/image/catalog/Blog/999/6.6-5.png">
// 图片放大展示框
<div class="modal myModal" id="myModal">;
<span class="Modal_close" onclick="Modalclose()">×</span>
<img class="modal-content Modal_img" alt="">
</div>
css部分,设置展示框的位置,可以是居于屏幕中间,设置图片出来的动画,再加一个关闭按钮的样式;
.modal {
display: none;
position: fixed;
z-index: 100;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0, 0, 0);
background-color: rgba(0, 0, 0, 0.9);
}
/* Modal Content (image) */
.modal-content {
margin: auto;
display: block;
width: 50%;
max-width: 500px;
}
.egg_btn{cursor: pointer;}
/* Add Animation */
.modal-content {
-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.1)
}
to {
transform: scale(1)
}
}
/* The Close Button */
.Modal_close {
position: absolute;
top: 15px;
right: 35px;
color: #f1f1f1;
font-size: 40px;
font-weight: bold;
transition: 0.3s;
}
.Modal_close:hover,
.Modal_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%;
}
}
js部分,点击放大事件,首先是显示模态框,然后获取当前点击的img标签的data-img的值,同时进行赋值。还有一个关闭事件,直接关闭模态框。
// 点击放大
function imgModelClick(that) {
$("#myModal").show();
$(".Modal_img").attr("src", $(that).attr("Nimg"))
if ($(that).attr("alt"));
$(".Modal_img").attr("alt", $(that).attr("alt"))
}
//关闭
function Modalclose() {
$("#myModal").hide();
}