一个手写的Vue放大镜,复制即可使用
一个手写的vue放大镜
组件使用less,请确保已安装loader
本组件为放大镜组件,传参列表为:
width: 必传,设置放大镜的宽高(正方形),放大区域等同,放大倍数为2倍
picList:必传,传入图片列表
使用示例:
script:
import mirror from 'xx/mirror'
export default {
components:{
mirror
},
data(){
return {
width:300,
picList:[
xxxxxx,
xxxxxx
],
}
}
}
html:
<mirror :width="width" :picList="picList" />
详细代码:
HTML:
<template>
<div class="mirror">
<div class="wrap" :style="{width:width+'px',height:width+'px'}" >
<div ref="truth" :style="{width:'100%',height:'100%'}" @mousemove="move" @mouseenter="showMagnify" @mouseleave="hideMagnify">
<div class="mask" ref="mask" v-show = "showMask" :style="{width:width/2+'px',height:width/2+'px',left:maskPosition.x+'px',top:maskPosition.y+'px'}"></div>
<img :src="picList[picIndex]" draggable="false"/>
</div>
<div class="virtual" ref="virtual" v-if = "isShowVirtual" :style="{width:width+'px',height:width+'px'}" >
<div class="big" ref = "bigPic" :style="{position:'absolute',width:2*width+'px',height:2*width+'px',backgroundSize:'100% 100%',backgroundImage:`url(${picList[picIndex]})`,left:percent.x, top:percent.y}" >
</div>
</div>
</div>
<ul class="picList" :style="{width:width+'px'}">
<li v-for = "(item,index) in picList" :class="{now:index==picIndex}" :data-index="index" :key ="item" @mouseenter="changeIndex">
<img :src ="item" />
</li>
</ul>
</div>
</template>
JS:
export default {
props:['width','picList'],//宽度是用来给放大镜的
data(){
return {
picIndex:0,
isShowVirtual:false,
showMask:false,
maskPosition:{},
percent:{},
}
},
methods:{
computedOffset(obj,prop){ //计算元素到body的绝对位置
if(obj==document.body || obj.offsetParent == document.body){
return parseInt(obj[prop])
}
return parseInt(obj[prop])+this.computedOffset(obj.offsetParent,prop)
},
changeIndex(e){
this.picIndex = e.target.dataset.index
},
showMagnify(e){
this.showMask=true;
this.isShowVirtual = true;
},
hideMagnify(){
this.isShowVirtual=false;
this.showMask=false
},
computePosition(e){
let x = e.pageX,y = e.pageY;
let mask = this.$refs.mask;
let truth = this.$refs.truth;
let virtual = this.$refs.virtual;
let bigPic = this.$refs.bigPic;
x = x-this.computedOffset(truth,'offsetLeft') -mask.offsetWidth/2;
y = y-this.computedOffset(truth,'offsetTop')- mask.offsetHeight/2;
if(x<=0) {
x=0
}else if(x>truth.offsetWidth - mask.offsetWidth){
x = truth.offsetWidth/2
}
if(y<=0){
y=0;
}
else if(y>truth.offsetHeight - mask.offsetHeight){
y = truth.offsetHeight/2
}
this.maskPosition = {
x,y
}
//计算比例
this.percent={
x:-x/(truth.offsetWidth-mask.offsetWidth)*(bigPic.offsetWidth - virtual.offsetWidth)+'px',
y:-y/(truth.offsetHeight-mask.offsetHeight)*(bigPic.offsetHeight - virtual.offsetHeight)+'px'
}
},
move(e){
this.computePosition(e)
}
}
}
CSS:
<style lang="less" scoped>
.now{
border-color: cyan !important;
}
.mirror{
width:100%;
.wrap{
user-select: none;
margin-bottom: 20px;
position: relative;
background-color: #fff;
border:1px solid gray;
box-sizing:border-box;
cursor: pointer;
img{
width:100%;
height:100%;
}
.virtual{
overflow:hidden;
width:100%;
height:100%;
position:absolute;
left:calc(100% + 10px);
top:0;
background-repeat:no-repeat
}
.mask{
position: absolute;
background-image: url('https://img-tmdetail.alicdn.com/tps/i4/T12pdtXaldXXXXXXXX-2-2.png');
background-repeat:repeat;
cursor: move;
}
}
.picList{
width:100%;
display: flex;
justify-content: space-between;
flex-wrap:wrap;
li{
width:50px;
height:50px;
margin:5px;
border:1px solid transparent;
box-sizing: border-box;
img{
width:100%;
height:100%
}
}
}
.picList:after{
content:"";
flex:auto;
}
}
</style>
可直接复制文件内容至项目使用,文件地址:https://blog-static.cnblogs.com/files/hhyf/mirror.vue.js
效果
一个手写的Vue放大镜,复制即可使用的更多相关文章
- 最新用WPF为触摸屏写了一个手写程序,双格输入的
原文:最新用WPF为触摸屏写了一个手写程序,双格输入的 双格输入可以提高手写速度,当前字写完以后可以自动识别提交,写下一个字.这样比单格手写速度提高一倍.特别适合触摸屏程序使用 界面如下: 程序如下: ...
- 分享一个自己写的vue多语言插件smart-vue-i18n
前言 目前有比较成熟的方案(vue-i18n)了解了下,并且实用了一下感觉对于我在使用的项目来说略显臃肿,功能比较多,所以压缩的会比较大,在移动端不太适合所以自己花一天时间撸了一个vue多语言插件,压 ...
- 手写实现vue的MVVM响应式原理
文中应用到的数据名词: MVVM ------------------ 视图-----模型----视图模型 三者与 Vue 的对应:view 对应 te ...
- 一个手写的 http client
public class HTTPClient { public static final String GET = "GET"; public static final Stri ...
- 浅析MyBatis(二):手写一个自己的MyBatis简单框架
在上一篇文章中,我们由一个快速案例剖析了 MyBatis 的整体架构与整体运行流程,在本篇文章中笔者会根据 MyBatis 的运行流程手写一个自定义 MyBatis 简单框架,在实践中加深对 MyBa ...
- 手写 Vue 系列 之 Vue1.x
前言 前面我们用 12 篇文章详细讲解了 Vue2 的框架源码.接下来我们就开始手写 Vue 系列,写一个自己的 Vue 框架,用最简单的代码实现 Vue 的核心功能,进一步理解 Vue 核心原理. ...
- 依据ECMA规范,手写一个bind函数
Function.prototype.bind 函数,参见ECMA规范地址 如题,这次来实现一个boundFunction函数,不挂载在Function.prototype上,而是一个单独声明的函数. ...
- AI应用开发实战 - 手写识别应用入门
AI应用开发实战 - 手写识别应用入门 手写体识别的应用已经非常流行了,如输入法,图片中的文字识别等.但对于大多数开发人员来说,如何实现这样的一个应用,还是会感觉无从下手.本文从简单的MNIST训练出 ...
- Caffe系列4——基于Caffe的MNIST数据集训练与测试(手把手教你使用Lenet识别手写字体)
基于Caffe的MNIST数据集训练与测试 原创:转载请注明https://www.cnblogs.com/xiaoboge/p/10688926.html 摘要 在前面的博文中,我详细介绍了Caf ...
随机推荐
- linux初学者-sshd服务
linux初学者-sshd服务 在linux系统操作中,经常需要连接其他的主机,连接其他主机的服务是openssh-server,它的功能是让远程主机可以通过网络访问sshd服务,开始一个安全s ...
- Java常用命令及参数
Java的基本指令参数 javac [-d 目录|-verbose] file java [-classpath(cp) dir] file jar -zcvf dir file javap [-pr ...
- Linux基础之bash shell介绍及基本特性
今天继续讲Linux基础知识,内容是关于bash shell的.分享以下bash shell的相关知识,例如基本特性等. 1.8)bash shell的介绍 1.8.1)什么是bash shell ...
- Apache和Spring提供的StopWatch执行时间监视器
相关阅读 [小家java]java5新特性(简述十大新特性) 重要一跃 [小家java]java6新特性(简述十大新特性) 鸡肋升级 [小家java]java7新特性(简述八大新特性) 不温不火 [小 ...
- css3系列之transform详解translate
translate translate这个参数的,是transform 身上的,那么它有什么用呢? 其实他的作用很简单,就是平移,参考自己的位置来平移 translate() translateX() ...
- WebRTC:一个视频聊天的简单例子
相关API简介 在前面的章节中,已经对WebRTC相关的重要知识点进行了介绍,包括涉及的网络协议.会话描述协议.如何进行网络穿透等,剩下的就是WebRTC的API了. WebRTC通信相关的API非常 ...
- kubernetes CRD开发指南
扩展kubernetes两个最常用最需要掌握的东西:自定义资源CRD 和 adminsion webhook, 本文教你如何十分钟掌握CRD开发. kubernetes允许用户自定义自己的资源对象,就 ...
- 搞定java String校招面试题
今天大致的阅读了String类的源码,并刷了常见的面试题,在此做个笔记. 面试题一:判断下列程序运行结果 package String_test; public class test_1 { publ ...
- springmvc原理详解(手写springmvc)
最近在复习框架 在快看小说网搜了写资料 和原理 今天总结一下 希望能加深点映像 不足之处请大家指出 我就不画流程图了 直接通过代码来了解springmvc的运行机制和原理 回想用springmvc用 ...
- 大陆争霸[SDOI2010]带限制最短路
只要你有无限个自爆机器人,你就能为所欲为 斯普林·布拉泽 [题目描述] 略 一句话题意: 杰森国有 \(N\) 个城市,由 \(M\) 条单向道 路连接.杰森国的首都是城市 \(N\).你只需摧毁杰森 ...