vue图片放大镜效果
原作者地址: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>
效果图:

vue图片放大镜效果的更多相关文章
- vue 图片放大镜效果
插件名称:vue-photo-zoom-pro https://github.com/Mater1996/vue-photo-zoom-pro 效果图 使用: <template> &l ...
- Magnifier.js - 支持鼠标滚轮缩放的图片放大镜效果
Magnifier.js 是一个 JavaScript 库,能够帮助你在图像上实现放大镜效果,支持使用鼠标滚轮放大/缩小功能.放大的图像可以显示在镜头本身或它的外部容器中.Magnifier.js 使 ...
- WPF设置VistualBrush的Visual属性制作图片放大镜效果
原文:WPF设置VistualBrush的Visual属性制作图片放大镜效果 效果图片:原理:设置VistualBrush的Visual属性,利用它的Viewbox属性进行缩放. XAML代码:// ...
- 原生javascript实现图片放大镜效果
当我们在电商网站上购买商品时,经常会看到这样一种效果,当我们把鼠标放到我们浏览的商品图片上时,会出现类似放大镜一样的一定区域的放大效果,方便消费者观察商品.今天我对这一技术,进行简单实现,实现图片放大 ...
- 【Demo】jQuery 图片放大镜效果——模仿淘宝图片放大效果
实现功能: 模仿淘宝图片放大效果,鼠标移动到小图片的某一处,放大镜对应显示大图片的相应位置. 实现效果: 实现代码: <!DOCTYPE html> <html> <he ...
- javascript图片放大镜效果展示
javascript图片放大镜效果展示 <!DOCTYPE html> <html> <head lang="en"> <meta cha ...
- canvas知识02:图片放大镜效果
效果截图: JS代码: <script> // 初始化canvas01和上下文环境 var cav01 = document.getElementById('cav01'); var cx ...
- jQuery实现图片放大镜效果
实现图片放大镜的原理: 给放大镜元素一个对应的html元素为<div class='right'> 设置这个div的宽高固定为某个值(350px,350px) 设置div的css为超出部分 ...
- 用css3的cursor:zoom-in/zoom-out实现微博看图片放大镜效果
1.前言 CSS3的出现解决了很多让人头疼的问题,至少我想很多童鞋都这样认为.css3的cursor属性大家用的应该是非常的多的,我想用的比较多的像cursor:pointer;cursor:help ...
随机推荐
- 喵哈哈村的魔法考试 Round #10 (Div.2) 题解
喵哈哈村与哗啦啦村的大战(一) 最大值就是全部+3,最小值就是全部-3,注意不能降为负数. #include<bits/stdc++.h> using namespace std; con ...
- C++ operator重载运算符和隐式转换功能的实现
C++ operator重载运算符和隐式转换功能的实现: #include <iostream> using namespace std; class OperatorTest { pub ...
- ASP.NET MVC 4 中的JSON数据交互
前台Ajax请求很多时候需要从后台获取JSON格式数据,一般有以下方式: 拼接字符串 return Content("{\"id\":\"1\",\& ...
- 《JavaScript-The Definitive Guide》读书笔记:函数定义和函数调用
定义函数 使用function关键字来定义函数,分为两种形式: 声明式函数定义: function add(m,n) { alert(m+n); } 这种方式等同于构造一个Function类的实例的方 ...
- axios 取消请求的方法
开发中遇到需要取消请求的功能,,点击终止查询可以取消开始查询请求,再次点击开始查询又可以进行查询. 解决方法:axios官方文档上的CancelToken,一开始用了这个api后,可以成功取消请求,但 ...
- 内核同步机制-RCU同步机制
转自:https://blog.csdn.net/nevil/article/details/7718375 转自http://www.360doc.com/content/09/0805/00/36 ...
- SpringMVC拦截器详解
拦截器是每个Web框架必备的功能,也是个老生常谈的主题了. 本文将分析SpringMVC的拦截器功能是如何设计的,让读者了解该功能设计的原理. 重要接口及类介绍 1. HandlerExecution ...
- version-script 控制 so的符号输出
http://blog.sina.com.cn/s/blog_493667730100csde.html https://stackoverflow.com/questions/8129782/ver ...
- Oracle 12c利用数据泵DataPump进行Oracle数据库备份
1.查看数据库版本 SQL> select version from v$instance; VERSION ----------------- 12.1.0.2.0 2.sysdba用户登录s ...
- composer 使用
#安装 composer curl -sS https://getcomposer.org/installer | php或直接下载 composer.phar( https://getcompose ...