前端自定义封装图片预览组件(支持多张图片预览 缩放):

devtools/2024/9/24 22:26:41/
htmledit_views">

封装图片预览组件:

<template><div ref="previewWrapper" class="image-preview"><div class="overlay" v-if="showOverlay" @click="closePreview"></div><div class="preview-container" v-wheelScale><img :src="currentImageUrl" alt="Preview Image" @load="imageLoaded" ref="previewImage"></div><div class="arrow arrow-left" @click="prevImage" :disabled="currentIndex === 0">&lt;</div><div class="arrow arrow-right" @click="nextImage" :disabled="currentIndex === images.length - 1">&gt;</div></div></template><script>export default {props: {images: {type: Array,required: true,},},data() {return {showOverlay: false,currentIndex: 0,currentImageUrl: '',scale: 1,initialMouseX: 0,initialScale: 1,isDragging: false,};},methods: {openPreview() {this.showOverlay = true;this.currentImageUrl = this.images[this.currentIndex];this.$refs.previewWrapper.style.display = 'flex';setTimeout(() => {this.$refs.previewWrapper.style.opacity = 1;}, 10);},closePreview() {this.showOverlay = false;setTimeout(() => {this.$refs.previewWrapper.style.opacity = 0;setTimeout(() => {this.$refs.previewWrapper.style.display = 'none';}, 0);}, 0);},nextImage() {this.currentIndex = (this.currentIndex + 1) % this.images.length;this.currentImageUrl = this.images[this.currentIndex];},prevImage() {this.currentIndex = (this.currentIndex - 1 + this.images.length) % this.images.length;this.currentImageUrl = this.images[this.currentIndex];},imageLoaded() {// 可以在此处调整图片的居中或其它布局逻辑},},mounted() {// 初始化时隐藏预览层this.$refs.previewWrapper.style.display = 'none';},};</script><style scoped>.image-preview {position: fixed;top: 0;left: 0;right: 0;bottom: 0;z-index: 999;display: none;justify-content: center;align-items: center;opacity: 0;transition: opacity 0.7s ease-in-out;}.overlay {position: absolute;top: 0;left: 0;width: 100%;height: 100%;background-color: rgba(0, 0, 0, 0.8);cursor: pointer;}.preview-container {position: relative;text-align: center;overflow: hidden;max-width: 90%;max-height: 90vh;}.arrow {width: 50px;height: 50px;position: absolute;top: 50%;transform: translateY(-50%);background: rgba(255, 255, 255, 0.8);padding: 10px;border-radius: 50%;cursor: pointer;z-index: 1;opacity: 0.5;transition: opacity 0.3s;border: none;font-size: 20px;line-height: 50px;}.arrow:hover {opacity: 1;}.arrow-left {left: 10px;}.arrow-right {right: 10px;}</style>

图片放大缩小依靠自定义指令实现:

自定义指定代码:src/utils/scale.js

export const initVWheelScale = (Vue) => {Vue.directive("wheelScale", (el, binding) => {const {maxScale = 5,minScale = 0.5,initScale = 1,cssVarName = "--scale",} = binding.arg || {}let currentScale = initScale || el.style.getPropertyValue(cssVarName) || 1setWheelScale(binding, {el,cssVarName,currentScale,minScale,maxScale,})if (el) {el.onwheel = (e) => {currentScale = el.style.getPropertyValue(cssVarName) || 1if (e.wheelDelta > 0) {currentScale = currentScale * 1 + 0.1} else {currentScale = currentScale * 1 - 0.1}setWheelScale(binding, {el,cssVarName,currentScale,minScale,maxScale,})}}})}// 设置 --scale 变量 缩放比例const setVarScale = (el, cssVarName, currentScale, minScale, maxScale) => {// 现在缩放范围if (currentScale > maxScale) {currentScale = maxScale} else if (currentScale < minScale) {currentScale = minScale}let cssText = el.style.cssTextlet cssTextList = cssText.split(";")let isExist = falselet isExistIndex = -1for (let index = 0; index < cssTextList.length; index++) {const element = cssTextList[index]if (element.includes(cssVarName + ":")) {isExist = trueisExistIndex = indexbreak}}if (isExist) {cssTextList[isExistIndex] = `--scale: ${currentScale}`} else {cssTextList.push(`--scale: ${currentScale}`)//   el.setAttribute("style", `--scale: ${currentScale}`)}cssText = cssTextList.join(";")el.style.cssText = cssTextreturn currentScale}// 设置 style.transformconst setTransformCss = (el, cssVarName) => {let transformCssString = el.style.transformlet regScaleGlobal = /scale\(.*?[ )]*[)]+[ ]*/g //匹配 Scale属性 全局if (regScaleGlobal.test(transformCssString)) {transformCssString = transformCssString.replace(regScaleGlobal,` scale(var(${cssVarName})) `)} else {transformCssString += " " + `scale(var(${cssVarName}))`}el.style.transform = transformCssString}export const setWheelScale = (binding = {}, options) => {const { el, cssVarName, currentScale, minScale, maxScale } = optionsconst nowScale = setVarScale(el, cssVarName, currentScale, minScale, maxScale)setTransformCss(el, cssVarName)// 缩放改变回调函数const wheelScaleHandle = binding.value || nullif (wheelScaleHandle instanceof Function) {wheelScaleHandle({el,cssVarName,maxScale,minScale,currentScale: nowScale,setScale: (_scale) => {setWheelScale(binding, { ...options, currentScale: _scale })},binding,})}}

main.js中全局注册自定义指令:

import { initVWheelScale} from "@/utils/scale.js"
initVWheelScale(Vue)

在图片预览组件中使用:

组件的使用:

<template><div><!-- ...其他内容 --><button @click="openPreview">预览图片</button><image-preview :images="imageList" ref="imagePreview"></image-preview></div>
</template><script>
import ImagePreview from '@/components/ImagePreview.vue'; // 引入你的图片预览组件export default {components: {ImagePreview,},data() {return {imageList: ['https://img1.baidu.com/it/u=582697934,2565184993&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=539','https://img2.baidu.com/it/u=3519181745,2349627299&fm=253&fmt=auto&app=120&f=JPEG?w=750&h=500',// 更多图片路径],};},methods: {openPreview() {this.$refs.imagePreview.openPreview();},},
};
</script>

效果:


http://www.ppmy.cn/devtools/23711.html

相关文章

对2023年图灵奖揭晓看法

2023年图灵奖揭晓&#xff0c;你怎么看&#xff1f; 2023年图灵奖&#xff0c;最近刚刚颁给普林斯顿数学教授 Avi Wigderson&#xff01;作为理论计算机科学领域的领军人物&#xff0c;他对于理解计算中的随机性和伪随机性的作用&#xff0c;作出了开创性贡献。这些贡献不仅推…

C语言内存函数

C语言内存函数 个人主页&#xff1a;大白的编程日记 个人专栏&#xff1a;大白的编程日记 文章目录 C语言内存函数前言一.memcpy函数1.1memcpy函数的使用1.2memcpy的模拟实现 二.memmove2.1memmove的使用2.2memmove的模拟实现 三.memse函数的使用3.1memset的使用3.2memset的模…

Python学习路线图及开源库和工具推荐

引言 Python作为一门易学易用且功能强大的编程语言&#xff0c;受到了广泛的欢迎和应用。作为一名想要学习Python的初学者&#xff0c;可能有些迷茫&#xff0c;不知道该如何入门&#xff0c;又该如何深入学习。本文将详细介绍Python的学习路线图&#xff0c;同时推荐一些高质…

编译报错 - Missing trailing comma comma-dangle or Missing semicolon semi

一、comma-dangle规则&#xff1a; 这种错误通常出现在使用代码格式检查工具&#xff08;如ESLint&#xff09;时&#xff0c;具体是在JSON或者JavaScript对象、数组的最后一个元素后面缺少了逗号&#xff08;trailing comma&#xff09;。在某些编码标准中&#xff0c;要求在…

srpingMVC基本使用

文章目录 1. springMVC基本功能(1) maven坐标导入(2) 编写表现层(3) springMVC配置类编写(4) 部署tomcat访问 2. 各种请求方法get请求post请求put请求delete请求请求参数提取 3. 请求参数接收(1) param参数接受封装到对象中 (2) 路劲参数接收集合接受时间类型接收json参数接收m…

数字工厂管理系统设备管理功能具有哪些作用

在现代化工业生产中&#xff0c;数字工厂管理系统已成为提升生产效率、优化资源配置、保障生产安全的重要工具。其中&#xff0c;设备管理功能作为数字工厂管理系统的核心组成部分&#xff0c;对于提升设备利用率、降低维护成本、实现智能化生产等方面具有不可或缺的作用。本文…

使用python写一个识别人脸

人脸识别的原理涉及多个领域&#xff0c;包括图像处理、特征提取和机器学习等。以下是一个简化的概述&#xff0c;并展示了如何使用Python和OpenCV库来实现基本的人脸识别。 人脸识别原理概述 图像预处理&#xff1a;首先&#xff0c;我们需要对输入的图像进行预处理&#xf…

webpack前端性能优化- HappyPack多线程打包-打包速度提升n倍

HappyPack 由于运行在 Node.js 之上的 webpack 是单线程模型的&#xff0c;我们需要 webpack 能同一时间处理多个任务&#xff0c;发挥多核 CPU 电脑的威力 HappyPack 插件就能实现多线程打包&#xff0c;它把任务分解给多个子进程去并发的执行&#xff0c;子进程处理完后再把…