vue2自定义封装图片预览组件

news/2024/7/10 0:45:26 标签: 前端, vue

前言:预览图片现在已经有成熟的组件了,比如element ui的图片预览功能,但是现实开发过程中,element ui图片预览已经不满足需求了,比如涉及预览时删除图片以及下载图片

自定义封装图片预览组件

功能:滚轮滚动图片放大、还原图片、左旋转、右旋转、上一张、下一张、删除图片、预览大图
参数传入:是否显示图片、图片的url、图片预览图的宽度、图片预览图的宽度、预览列表

1、布局

这里的布局根据需求有两种,一种是显示当前第一张图片,点击预览也可切换下一张
另外一种是不显示图片,点击查看才可显示,这里通过一个变量isShowImg控制

  <div class="image-wrap">
    <div @click="onPreview" :class="[isShowImg ? '' : 'isShowImg']">
      <img
        v-if="isShowImg"
        class="thumb-image"
        :src="src"
        :style="{
          width: width,
          height: height
        }"
      />
    </div>
    <div class="mask" v-if="isShow">
      <div class="mask-image__wrap">
        <img
          id="customImage"
          class="origin-image"
          :src="originUrl"
          :style="styleObj"
          @wheel="onWheel"
        />
      </div>
      <div class="action">
        <button class="btn-item" @click="onZoom('in')">放大</button>
        <button class="btn-item" @click="onZoom('out')">缩小</button>
        <button class="btn-item" @click="onReset">还原</button>
        <button class="btn-item" @click="onRotate('left')">左转</button>
        <button class="btn-item" @click="onRotate('right')">右转</button>
        <button class="btn-item" @click="isShow = false">关闭</button>
        <button class="btn-item" @click="onPrev" v-if="isOnPrev">上一张</button>
        <button class="btn-item" @click="onNext" v-if="isOnNext">下一张</button>
        <button class="btn-item" @click="onDelete" v-if="isButton">删除</button>
      </div>
      <div class="close-wrap">
        <button class="btn-item" @click="isShow = false">关闭</button>
      </div>
    </div>
  </div>

2、预览图片

isShow 控制大图显示隐藏
previewList 为当前图片列表

    //  预览大图
    onPreview() {
      this.isShow = true;
      this.curIndex = this.index;
      if (this.previewList.length === 0) {
        this.originUrl = this.src;
      } else {
        if (this.curIndex < this.previewList.length) {
          this.originUrl = this.previewList[this.curIndex];
        }
      }
    }

3、放大缩小图片

使用鼠标滚轮事件,或者点击放大缩小按钮也可实现
原理:改变图片样式,使用css3

 onZoom(type) {
      if (this.isDone) return;
      this.isDone = true;
      const timer = setTimeout(() => {
        if (type === "in" && this.scale < MAX_SCALE) {
          this.scale = (this.scale * 100 + ZOOM_BASE * 100) / 100;
        } else if (type === "out" && this.scale > MIN_SCALE) {
          this.scale = (this.scale * 100 - ZOOM_BASE * 100) / 100;
        }
        this.styleObj = {
          transition: `transform ${this.interVal}`,
          transform: `scale(${this.scale}) rotate(${this.rotate}deg)`
        };
        this.isDone = false;
        clearTimeout(timer);
      }, INTERVAL);
    },
    //  鼠标滚轮事件
    onWheel(event) {
      event.deltaY < 0 ? this.onZoom("in") : this.onZoom("out");
    },

4、旋转

原理:改变图片样式,使用css3

    onRotate(type) {
      if (this.isDone) return;
      this.isDone = true;
      const timer = setTimeout(() => {
        if (type === "left") {
          this.rotate = this.rotate - ROTATE_BASE;
        } else {
          this.rotate = this.rotate + ROTATE_BASE;
        }
        this.styleObj = {
          transition: `transform ${this.interVal}`,
          transform: `scale(${this.scale}) rotate(${this.rotate}deg)`
        };
        this.isDone = false;
        clearTimeout(timer);
      }, INTERVAL);
    },

5、切换图片

原理,这个就比较容易了,主要是切换图片索引就可以

    onPrev() {
      if (this.curIndex <= 0) return;
      this.curIndex -= 1;
      this.originUrl = this.previewList[this.curIndex];
      this.onReset();
    },
    //  下一张
    onNext() {
      if (this.curIndex >= this.previewList.length - 1) return;
      this.curIndex += 1;
      this.originUrl = this.previewList[this.curIndex];
      this.onReset();
    },

6、删除图片

这里主要使用子传父来实现,这里就不描述了

7、使用

在某一个vue文件中引入并传入图片列表即可

 <PreviewImg
    :isShowImg="false"
     :previewList="list"
   />

这样就实现啦,其实原理很简单


http://www.niftyadmin.cn/n/5332896.html

相关文章

美媒:OpenAI删除了禁止其技术被用于军事用途的条款

据美国调查新闻网站“拦截者”&#xff08;The Intercept&#xff09;1月12日报道&#xff0c;美国知名人工智能企业、ChatGPT母公司OpenAI近日悄悄修改了其产品的使用条款&#xff0c;删除了禁止将OpenAI技术用于军事用途的条文。 报道称&#xff0c;在今年1月10日之前&#…

如何选择合适的探针台

随着电子技术的不断发展&#xff0c;越来越多的精密细小器件逐步开放和增长&#xff0c;作为对细小器件检测的bi备仪器—探针台也显得越来越重要。探针台可以将待测器件进行放大几十到上千倍&#xff0c;可将微观层面的东西搬到宏观上来进行研究&#xff0c;连接测试仪器仪表进…

conda install命令无法安装pytorch

由于网络问题&#xff0c;直接采用conda install命令可能无法直接下载对应的cuda包。 方法一&#xff1a;采用pip命令替代 步骤1&#xff1a;切换pip的源为国内源&#xff1a; 若只是临时切换&#xff1a; pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-p…

(二十)Flask之上下文管理第一篇(粗糙缕一遍源码)

每篇前言&#xff1a; &#x1f3c6;&#x1f3c6;作者介绍&#xff1a;【孤寒者】—CSDN全栈领域优质创作者、HDZ核心组成员、华为云享专家Python全栈领域博主、CSDN原力计划作者 &#x1f525;&#x1f525;本文已收录于Flask框架从入门到实战专栏&#xff1a;《Flask框架从入…

【算法】斐波那契数列 [递推,矩阵快速幂]

方法一. 递推 class Solution { public:int fib(int n) {int MOD 1e9 7;if (n < 2) return n;int p 0, q 0, r 1;for (int i 2; i < n; i) {p q;q r;r (p q) % MOD;}return r;} }; 方法二&#xff1a;矩阵快速幂 class Solution { public:const int MOD 1e…

[面试题~]Golang

3. 数组和切片 3.1 数组和切片的区别 Go语言中数组是固定长度的&#xff0c;不能动态扩容&#xff0c;在编译期就会确定大小。 切片是一种数据结构&#xff0c;包含一个底层数组的指针&#xff0c;当前切片个数 len 以及切片的最大容量 cap&#xff0c; 描述的是一块数组。 …

PieCloudDB 多种压缩手段:降低数据库存储成本

随着企业数据规模的不断增长&#xff0c;数据库用户的关键业务系统面临着存储和处理庞大数据量的挑战。这不仅意味着更昂贵的 IT、数据成本&#xff0c;还包括更多的资源消耗和管理复杂性。为了满足企业的需求&#xff0c;云原生虚拟数仓 PieCloudDB 通过一系列的创新技术手段帮…

[足式机器人]Part2 Dr. CAN学习笔记- Kalman Filter卡尔曼滤波器Ch05-3+4

本文仅供学习使用 本文参考&#xff1a; B站&#xff1a;DR_CAN Dr. CAN学习笔记 - Kalman Filter卡尔曼滤波器 Ch05-34 3. Step by step : Deriation of Kalmen Gain 卡尔曼增益/因数 详细推导4. Priori/Posterrori error Covariance Martix 误差协方差矩阵 3. Step by step :…