移动端图片预览效果

实现方式
<template>
<div>
<!-- 移动端弹出框 -->
<div id="myModal" class="modal">
<div class="modal-content">
<img class="modal-img-wrap" id="img01">
</div>
<div id="caption"></div>
</div>
<a-button class="btn" type="primary" @click="handleImg">图片预览</a-button>
</div>
</tempalte>
<script>
export default {
data() {
return {
url: require('@/assets/kefu.png'),
alt: "长按/截图保存,微信识别二维码或者关注公众号“xxx”"
}
},
methods: {
handleImg() {
// 获取模态窗口
var modal = document.getElementById('myModal');
// 获取图片模态框,alt 属性作为图片弹出中文本描述
// var img = document.getElementById('myImg');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
if(document.body.clientWidth < 768){ // 判断是否移动端
modal.style.display = "block";
}
modalImg.src = this.url;
modalImg.alt = this.alt;
captionText.innerHTML = this.alt;
// 点击 <modal> 元素上的 (x), 关闭模态框
modal.onclick = function() {
modal.style.display = "none";
};
}
},
}
</script>
<style scoped>
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding: 150px 30px 0; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgba(0,0,0,0.7); /* Black w/ opacity */
}
.modal-content {
background: #FFFFFF;
border-radius: 5px;
padding: 16px;
}
/* Modal Content (image) */
.modal-img-wrap {
margin: auto;
display: block;
}
/* Caption of Modal Image */
#caption {
margin: auto;
display: block;
max-width: 700px;
padding: 20px 0;
width: 214px;
text-align: justify;
font-size: 14px;
font-weight: 400;
color: #FFFFFF;
line-height: 20px;
}
/* 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.1)}
to {transform: scale(1)}
}
</style>
参考链接
图片modal