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 ...
随机推荐
- 网络编程(1)—TCP
java.net 包中提供了两种常见的网络协议的支持: TCP:TCP 是传输控制协议的缩写,它保障了两个应用程序之间的可靠通信.通常用于互联网协议,被称 TCP / IP. TCP协议: 使用TCP ...
- 什么是SASS
一.什么是SASS SASS是一种CSS的开发工具,提供了许多便利的写法,大大节省了设计者的时间,使得CSS的开发,变得简单和可维护. 本文总结了SASS的主要用法.我的目标是,有了这篇文章,日常的一 ...
- centos 6.4配置samba+ldap认证
原文地址:http://www.centoscn.com/image-text/config/2015/0716/5866.html 1. 什么是samba Samba服务类似于windows上的共 ...
- CentOS 6.9下安装PostgreSQL
操作系统:CentOS6.9_x64 PostgreSQL官方网址: https://www.postgresql.org/ 安装数据库 使用如下命令: yum install postgresql- ...
- 四舍五入函数ROUND(x,y)
四舍五入函数ROUND(x,y) 参数: x:数据 y:需要保留的小数点位数 ROUND(x,y)函数返回最接近于参数x的数,其值保留到小数点后面y位,若y为负值,则将保留x值到小数点左边y位. my ...
- Excel如何固定表头,任意一行
在日常Excel操作中,有时候内容比较多,需要将表头固定才能方便查看.那么,该如何固定表头呢?或者说如何固定任意一行我们制定的呢?下面以Excel2013进行详细的步骤讲解. 首先打开需要操作的Exc ...
- 基于Centos体验自然语言处理 by Python SDK
系统要求: CentOS 7.2 64 位操作系统 准备工作 获取 SecretId 和 SecretKey 前往 密钥管理 页面获取你的 SecretId 和 SecretKey 信息,这些信息将会 ...
- C#反射实现
一.反射概念: 1.概念: 反射,通俗的讲就是我们在只知道一个对象的外部而不了解内部结构的情况下,通过反射这个技术可以使我们明确这个对象的内部实现. 在.NET中,反射是重要的机制,它可以动态的分析程 ...
- [Aaronyang] 写给自己的WPF4.5 笔记6[三巴掌-大数据加载与WPF4.5 验证体系详解 2/3]
我要做回自己--Aaronyang的博客(www.ayjs.net) 博客摘要: Virtualizing虚拟化DEMO 和 大数据加载的思路及相关知识 WPF数据提供者的使用ObjectDataPr ...
- Effective Java 第三版——46. 优先考虑流中无副作用的函数
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...