当前位置: 首页 > news >正文

vue 放大镜(简易)

目录

zoom组件

<template><div class="pic-img"><div class="img-container"><img ref="img" @load="imgLoaded" :src="url" :style="overlayStyle" @error="imgerrorfun"/><div class="overlay"@mousemove.stop="!moveEvent && mouseMove($event)"@mouseout.stop="!leaveEvent && mouseLeave($event)":style="overlayStyle"></div><divv-if="!hideZoom && imgLoadedFlag &&!hideSelector":class="['img-selector', {'circle': type === 'circle'}]":style="[imgSelectorStyle, imgSelectorSize, imgSelectorPosition, !outShow && imgBg, !outShow && imgBgSize, !outShow && imgBgPosition]"><slot></slot></div><divv-if="outShow"v-show="!hideOutShow":class="['img-out-show', {'base-line': baseline}]":style="[imgOutShowSize, imgOutShowPosition, imgBg, imgBgSize, imgBgPosition]"><div v-if="pointer" class="img-selector-point"></div></div></div></div></template><script>
export default {name: "vue-photo-zoom-pro",props: {url: { type: String, default: require('@/assets/images/default-zoom.jpg') }, // 图片地址highUrl: String, // 更清晰的图片,若不提供会采用 urlwidth: { type: Number, default: 168 }, // 内部放大区域宽度type: { type: String, default: "square", validator: function(value) { return ["circle", "square"].indexOf(value) !== -1 } },selectorStyle: { type: Object, default() { return {} } },outShowStyle: {},scale: { type: Number, default: 3 }, // 放大倍数moveEvent: { type: [Object, MouseEvent], default: null }, // 当需要在外部监听移动事件时,请通过该字段传入事件(必须包含 pageX,pageY,clientY),这将禁用内部移动监听leaveEvent: { type: [Object, MouseEvent], default: null }, // 当需要在外部监听离开事件时,请通过该字段传入事件hideZoom: { type: Boolean, default: false }, // 隐藏放大镜outShow: { type: Boolean, default: false }, // 是否外置放大镜pointer: { type: Boolean, default: false }, // 外部区域的中心点baseline: { type: Boolean, default: false }, // 外部区域的基线overlayStyle:{ type: String, default: 'width:100%;height:100%' },},computed: {addWidth() { return !this.outShow ? (this.width / 2) * (1 - this.scale) : 0 },imgSelectorPosition() {let { top, left } = this.selector;return { top: `${top}px`, left: `${left}px` };},imgSelectorSize() {let width = this.selector.width;return { width: `${width}px`, height: `${width}px` };},imgSelectorStyle() {return this.selectorStyle;},imgOutShowSize() {let { scale, selector: { width } } = this;return { width: `${width * scale}px`, height: `${width * scale}px` };},imgOutShowPosition() {return { top: `${this.outShowTop}px`, right: `${-8}px` };},imgBg() {return { backgroundImage: `url(${this.highUrl || this.url})` };},imgBgSize() {let { scale, imgInfo: { height, width } } = this;return { backgroundSize: `${width * scale}px ${height * scale}px` };},imgBgPosition() {let { bgLeft, bgTop } = this.selector;return { backgroundPosition: `${bgLeft}px ${bgTop}px` };},},data() {return {selector: {width: this.width,top: 0,left: 0,bgTop: 0,bgLeft: 0,rightBound: 0,bottomBound: 0,absoluteLeft: 0,absoluteTop: 0},imgInfo: {},$img: null,screenWidth: document.body.clientWidth,outShowInitTop: 0,outShowTop: 0,hideOutShow: true,imgLoadedFlag: false,hideSelector: false,timer: null};},methods: {imgLoaded() {let imgInfo = this.$img.getBoundingClientRect();if (JSON.stringify(this.imgInfo) != JSON.stringify(imgInfo)) {this.imgInfo = imgInfo;this.initSelectorProperty(this.width);this.resetOutShowInitPosition();}if (!this.imgLoadedFlag) {this.imgLoadedFlag = true;this.$emit("created", imgInfo);}},mouseMove(e) {if (!this.hideZoom && this.imgLoadedFlag) {this.imgLoaded();const { pageX, pageY, clientY } = e;const { scale, selector, outShow, addWidth, outShowAutoScroll } = this;let { outShowInitTop } = this;const scrollTop = pageY - clientY;const { absoluteLeft, absoluteTop, rightBound, bottomBound } = selector;const x = pageX - absoluteLeft; // 选择器的x坐标 相对于图片const y = pageY - absoluteTop; // 选择器的y坐标if (outShow) {if (!outShowInitTop) {outShowInitTop = this.outShowInitTop = scrollTop + this.imgInfo.top;}this.hideOutShow && (this.hideOutShow = false);this.outShowTop =scrollTop > outShowInitTop ? scrollTop - outShowInitTop : 0;}this.hideSelector && (this.hideSelector = false);selector.top = y > 0 ? (y < bottomBound ? y : bottomBound) : 0;selector.left = x > 0 ? (x < rightBound ? x : rightBound) : 0;selector.bgLeft = addWidth - x * scale; // 选择器图片的坐标位置selector.bgTop = addWidth - y * scale;}},initSelectorProperty(selectorWidth) {const selectorHalfWidth = selectorWidth / 2;const selector = this.selector;const { width, height, left, top } = this.imgInfo;const { scrollLeft, scrollTop } = document.documentElement;selector.width = selectorWidth;selector.rightBound = width - selectorWidth;selector.bottomBound = height - selectorWidth;selector.absoluteLeft = left + selectorHalfWidth + scrollLeft;selector.absoluteTop = top + selectorHalfWidth + scrollTop;},mouseLeave() {this.hideSelector = true;if (this.outShow) {this.hideOutShow = true;}},reset() {Object.assign(this.selector, {top: 0,left: 0,bgLeft: 0,bgTop: 0});this.resetOutShowInitPosition();},resetOutShowInitPosition() {this.outShowInitTop = 0;},imgerrorfun(e){// let img = require('@/assets/vehicle_img/blank_vehicle.jpg')// this.url = img// e.target.src = img// e.target.οnerrοr= null}},watch: {moveEvent(e) {this.mouseMove(e);},leaveEvent(e) {this.mouseLeave(e);},url(n) {this.imgLoadedFlag = false;// let img = require('@/assets/vehicle_img/blank_vehicle.jpg')// if(n == img){//  this.outShow = false// }},width(n) {this.initSelectorProperty(n);},screenWidth(val) {if (!this.timer) {this.screenWidth = val;this.timer = setTimeout(() => {this.imgLoaded();clearTimeout(this.timer);this.timer = null;}, 400);}}},mounted() {this.$img = this.$refs["img"];},
};
</script><style scoped>.img-container {position: relative;}.overlay{cursor: crosshair;position: absolute;top: 0;left: 0;opacity: 0.5;z-index: 3;}.img-selector {position: absolute;cursor: crosshair;border: 1px solid rgba(0, 0, 0, 0.1);background-repeat: no-repeat;background-color: rgba(64, 64, 64, 0.6);}.img-selector.circle {border-radius: 50%;}.img-out-show {z-index: 5000;position: absolute;background-repeat: no-repeat;-webkit-background-size: cover;background-color: white;transform: translate(100%, 0);}.img-selector-point {position: absolute;width: 4px;height: 4px;top: 50%;left: 50%;transform: translate(-50%, -50%);background-color: black;}.img-out-show.base-line::after {position: absolute;box-sizing: border-box;content: "";width: 1px;border: 1px dashed rgba(0, 0, 0, 0.36);top: 0;bottom: 0;left: 50%;transform: translateX(-50%);}.img-out-show.base-line::before {position: absolute;box-sizing: border-box;content: "";height: 1px;border: 1px dashed rgba(0, 0, 0, 0.36);left: 0;right: 0;top: 50%;transform: translateY(-50%);}
</style>
<template><div class='wrapper'><div class='box'><!-- <pic-zoom:url="storageUrl":bigWidth="150":scale="3"overlayStyle="width: 100%;height: 400px;border-radius: 3px;"></pic-zoom> --><pic-zoom:width="150":url="url":scale="3"type="square":pointer="true":out-show="true"overlayStyle="width: 100%;height: 100%;"></pic-zoom></div></div>
</template><script>
import PicZoom from '@/components/PicZoom'
export default {components: { PicZoom },data() {return {url: 'https://bpic.588ku.com/illus_water_img/18/08/18/472f21d353b64c95b9994be6ed3b7274.jpg!/watermark/url/L3dhdGVyL3dhdGVyX2JhY2tfNDAwXzIwMC5wbmc=/repeat/true',}},
}
</script><style lang='scss' scoped>
.box {width: 400px;height: 400px;
}
</style>
http://www.lryc.cn/news/199431.html

相关文章:

  • 【计算机网络】第一章——概述
  • vue实现在页面拖拽放大缩小div并显示鼠标在div的坐标
  • LuatOS-SOC接口文档(air780E)-- io - io操作(扩展)
  • 【数据结构】线性表(六)堆栈:顺序栈及其基本操作(初始化、判空、判满、入栈、出栈、存取栈顶元素、清空栈)
  • 父组件可以监听到子组件的生命周期吗?
  • [开源]MIT开源协议,基于Vue3.x可视化拖拽编辑,页面生成工具
  • 【C++ Primer Plus学习记录】数组的替代品
  • JSP免杀马
  • 2023-10-16 node.js-调用python-记录
  • Kotlin 设置和获取协程名称
  • awk命令的使用
  • 【面试系列】Vue
  • 揭开MyBatis的神秘面纱:掌握动态代理在底层实现中的精髓
  • 结合领域驱动设计,理解TOGAF之架构方法论
  • Vue-vue项目Element-UI 表单组件内容要求判断
  • 【试题027】C语言宏定义小例题
  • 解决 sharp: Installation error: unable to verify the first certificate
  • 【Java】Java实现100万+ 的高并发、高性能设计
  • linux系统下,在vscode的命令行中调试python文件
  • DFS(分布式文件系统)与 DFSR(分布式文件系统复制)的区别
  • 丈母娘说:有编制的不如搞编程的
  • vue 部署后 405 not allowed
  • 【限时免费】20天拿下华为OD笔试之【回溯】2023Q1-硬件产品销售方案【欧弟算法】全网注释最详细分类最全的华为OD真题题解
  • 蜻蜓c影视追剧系统-多个小程序添加说明
  • linux 测试存储介质.emmc.nand.ufs.硬盘的读写速度方法
  • 基于 KubeSphere 部署 KubeBlocks 实现数据库自由
  • 图像识别-人脸识别与疲劳检测 - python opencv 计算机竞赛
  • 高性能计算与多模态处理的探索之旅:英伟达GH200性能优化与GPT-4V的算力加速未来
  • 代码随想录算法训练营Day59|动态规划17
  • 软考 系统架构设计师系列知识点之软件构件(2)