实现移动端图片预览效果,图片modal效果

本文详细介绍了在移动端如何通过Vue.js实现图片预览功能,包括创建弹出框、设置图片显示、alt属性描述以及动画效果。重点展示了HTML、CSS和JavaScript的配合使用,适合前端开发者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

移动端图片预览效果

实现方式

<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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值