vue实现图片预览功能

图片预览 点击放大缩小旋转 切换上下张图片

  1. 首先自己定义共用的一个组件
    在这里插入图片描述

  2. ViewImg.vue

<template>
  <div>
    <el-image-viewer
      v-if="isShow"
      :on-close="ViewerClose"
      :url-list="imgPathList"
    ></el-image-viewer>
  </div>
</template>

<script>
import ElImageViewer from "element-ui/packages/image/src/image-viewer";
export default {
  components: {
    ElImageViewer,
  },
  props: {
    imgPathList: {
      type: Array,
      default() {
        return [];
      },
    },
    isShow: {
      type: Boolean,
      default: false,
    },
  },

  data() {
    return {};
  },
  methods: {
    // 关闭弹窗
    ViewerClose() {
      this.$emit("update:isShow", false);
    },
  },
};
</script>

<style lang="less" scoped>
/deep/ .el-icon-circle-close:before {
  color: #fff;
}
</style>

3 在页面使用

       <img
            style="width:60px;height:60px;"
            :src="imagePath"
            alt=""
            @click="imgShowClick(imagePath,imagePathList)"
          />

imagePath 是用来显示的图片 imagePathList 是可能存在的图片列表

4 在页面的根目录引入组件

  <!-- 图片预览组件 -->
    <ViewImg :img-path-list="imgPathList" :is-show.sync="isShow" />

5 在页面的data中定义字段用来存储路径地址

data() {
    return {
      isShow: false,
      imgPathList: [], // 预览图片的地址
    };
  },

6 写图片预览方法

    imgShowClick(imgSrc, imgList) {
      const imgListFlag = imgList instanceof Array && imgList.length;
      if (imgSrc) {
        if (imgListFlag) {
          this.imgPathList = imgList;
        } else {
          this.imgPathList = [];
          this.imgPathList.push(imgSrc);
        }
        this.isShow = true;
      } else if (imgListFlag) {
        this.imgPathList = imgList;
        this.isShow = true;
      } else {
        this.$message.info("当前没有可预览的图片");
      }
    },

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A: 在模板中使用v-for指令,循环展示图片列表,并针对每张图片绑定事件,当用户点击图片时,调用showPreview方法展示窗口。 ``` <template> <div> <div v-for="(img, index) in imageList" :key="index"> <img :src="img.src" @click="showPreview(index)" /> </div> <div v-show="showPreviewFlag" class="preview-container"> <img :src="previewImage" /> <span class="close-btn" @click="closePreview">×</span> </div> </div> </template> ``` 在脚本中定义data中的数据和方法,如图片列表、窗口显示标志、图片地址等,并实现showPreview和closePreview方法。 ``` <script> export default { data() { return { imageList: [ { src: 'https://example.com/img/1.jpg' }, { src: 'https://example.com/img/2.jpg' }, { src: 'https://example.com/img/3.jpg' } ], showPreviewFlag: false, previewImage: '' } }, methods: { showPreview(index) { this.previewImage = this.imageList[index].src this.showPreviewFlag = true }, closePreview() { this.showPreviewFlag = false } } } </script> ``` 通过CSS样式设置窗口的样式,如定位、宽高、边框、背景等。 ``` <style> .preview-container { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, 0.5); display: flex; justify-content: center; align-items: center; } .preview-container img { max-width: 80%; max-height: 80%; border: 5px solid white; border-radius: 10px; } .close-btn { position: absolute; top: 10px; right: 10px; font-size: 30px; color: white; cursor: pointer; } </style> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值