原作者地址:https://github.com/lemontree2000/vue-magnify

经测试,原插件在使用时有bug,即在预览时进行鼠标滚动,导致遮罩层计算错误。我已修复该bug,特分享出来。

组件核心代码:

<template>
<div class="magnify">
<div class="preview-box" @mousemove="move($event)" @mouseout="out" ref="previewBox">
<img width="100%" :src="previewImg" alt="">
<div class="hover-box" ref="hoverBox"></div>
</div>
<div class="zoom-box" v-show="zoomVisiable" ref="zoomBox">
<img :src="zoomImg" alt="" ref="bigImg">
</div>
</div>
</template> <script>
function offset(el) {
let top = el.offsetTop;
let left = el.offsetLeft;
while (el.offsetParent) {
el = el.offsetParent;
top += el.offsetTop;
left += el.offsetLeft;
}
return {
left: left,
top: top
}
}
export default {
name: 'magnify',
props: {
previewImg: {
type: String,
default: ''
},
zoomImg: {
type: String,
default: ''
}
},
data() {
return {
zoomVisiable: false,
hoverVisiable: false
};
},
methods: {
out() {
this.zoomVisiable = false;
},
move(ev) {
this.init();
// 鼠标距离屏幕距离
let moveX = ev.clientX;
let moveY = ev.clientY;
// 大盒子距离顶部的距离
let offsetLeft = offset(this.oPreviewBox).left; let offsetTop = offset(this.oPreviewBox).top;
let left = moveX - offsetLeft - this.houverWidth / 2;
let top
if(this.scroll > 0) {
top = moveY - offsetTop + this.scroll - this.houverHeight / 2;
}else {
top = moveY - offsetTop - this.houverHeight / 2;
}
let maxWidth = this.pWidth - this.houverWidth;
let maxHeight = this.pWidth - this.houverHeight;
left = left < 0 ? 0 : left > maxWidth ? maxWidth : left;
top = top < 0 ? 0 : top > maxHeight ? maxHeight : top;
let percentX = left / (maxWidth);
let percentY = top / (maxHeight);
this.oHoverBox.style.left = left + 'px';
this.oHoverBox.style.top = top + 'px';
this.oBigImg.style.left = percentX * (this.bWidth - this.imgWidth) + 'px';
this.oBigImg.style.top = percentY * (this.bHeight - this.imgHeight) + 'px';
this.$emit('move', ev);
this.zoomVisiable = true;
},
init() {
this.oHoverBox = this.$refs.hoverBox;
this.oPreviewBox = this.$refs.previewBox;
this.oBigImg = this.$refs.bigImg;
this.imgBox = this.$refs.zoomBox;
this.houverWidth = this.oHoverBox.offsetWidth;
this.houverHeight = this.oHoverBox.offsetHeight;
this.pWidth = this.oPreviewBox.offsetWidth;
this.pHeight = this.oPreviewBox.offsetHeight;
this.imgWidth = this.oBigImg.offsetWidth;
this.imgHeight = this.oBigImg.offsetHeight;
this.bWidth = this.imgBox.offsetWidth;
this.bHeight = this.imgBox.offsetHeight;
this.scroll = document.documentElement.scrollTop || document.body.scrollTop;
}
}
};
</script> <!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
.magnify {
position: relative;
.preview-box {
width: 480px;
height: 480px;
border: 1px solid #dededd;
position: relative;
&:hover .hover-box{
display: block;
}
.hover-box {
position: absolute;
display: none;
left: 0;
top: 0;
width: 100px;
height: 100px;
border: 1px solid #545454;
background: url('https://img-tmdetail.alicdn.com/tps/i4/T12pdtXaldXXXXXXXX-2-2.png') repeat 0 0;
cursor: move;
user-select: none;
}
}
.zoom-box {
width: 480px;
height: 480px;
overflow: hidden;
position: absolute;
left: 485px;
border: 1px solid #dc7a7a;;
top: 0;
img {
position: absolute;
top: 0;
left: 0;
}
}
}
</style>

使用:

<template>
<div>
<my-magnify :previewImg="data.min" :zoomImg="data.max"></my-magnify>
</div>
</template>
<script>
import MyMagnify from "~/components/Magnify.vue";
export default {
data() {
return {
data: {
min:
"https://img.alicdn.com/imgextra/i3/2857774462/TB21fgcwwNlpuFjy0FfXXX3CpXa_!!2857774462.jpg_430x430q90.jpg",
max:
"https://img.alicdn.com/imgextra/i3/2857774462/TB21fgcwwNlpuFjy0FfXXX3CpXa_!!2857774462.jpg"
}
};
},
components: {
MyMagnify
}
}
</script>

效果图:

最新文章

  1. python之路-Mysql&amp;&amp;ORM
  2. Java 集合与队列的插入、删除在并发下的性能比较
  3. Bootstrap系列 -- 14. 表单控件输入框input
  4. #iPhone6与iPhone6Plus适配#如何在Xcode 6中创建 PCH 文件
  5. C/C++内存泄漏及检测 转
  6. 【Python】django权限管理
  7. javascript获取以及设置光标位置
  8. React-Native的基本控件属性方法
  9. jquery set selected for select element
  10. 【转】Android中intent传递对象和Bundle的用法
  11. WPF 自定义Button控件及样式
  12. openGL光源概念
  13. javascript实现图片延迟加载方法汇总(三种方法)
  14. 一个Android常用的组件收集
  15. pythone函数基础(10)MD5加密
  16. Linux编程 6 (查看进程 ps 及输出风格)
  17. Hdoj 1009.FatMouse&#39; Trade 题解
  18. [leetcode.com]算法题目 - Sqrt(x)
  19. Docker(六)-Dcoker仓库
  20. slf4j + log4j 是如何初始化的

热门文章

  1. __x__(24)0907第四天__ display 和 visibility
  2. 吴伯凡:VUCA时代的自我迭代
  3. 初识zookeeper(1)之zookeeper的安装及配置
  4. python测试开发django-45.xadmin添加小组件报错解决
  5. javax.crypto.BadPaddingException: Given final block not properly padded解决方案
  6. [数据结构与算法分析(Mark Allen Weiss)]二叉树的插入与删除 @ Python
  7. 还在为工作发愁?学JavaScript吧
  8. (转)热空气扭曲效果shader
  9. C#高级编程四十一天----用户定义的数据类型转换
  10. Oracle更改redo log大小 or 增加redo log组