一个手写的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放大镜,复制即可使用的更多相关文章

  1. 最新用WPF为触摸屏写了一个手写程序,双格输入的

    原文:最新用WPF为触摸屏写了一个手写程序,双格输入的 双格输入可以提高手写速度,当前字写完以后可以自动识别提交,写下一个字.这样比单格手写速度提高一倍.特别适合触摸屏程序使用 界面如下: 程序如下: ...

  2. 分享一个自己写的vue多语言插件smart-vue-i18n

    前言 目前有比较成熟的方案(vue-i18n)了解了下,并且实用了一下感觉对于我在使用的项目来说略显臃肿,功能比较多,所以压缩的会比较大,在移动端不太适合所以自己花一天时间撸了一个vue多语言插件,压 ...

  3. 手写实现vue的MVVM响应式原理

    文中应用到的数据名词: MVVM   ------------------        视图-----模型----视图模型                三者与 Vue 的对应:view 对应 te ...

  4. 一个手写的 http client

    public class HTTPClient { public static final String GET = "GET"; public static final Stri ...

  5. 浅析MyBatis(二):手写一个自己的MyBatis简单框架

    在上一篇文章中,我们由一个快速案例剖析了 MyBatis 的整体架构与整体运行流程,在本篇文章中笔者会根据 MyBatis 的运行流程手写一个自定义 MyBatis 简单框架,在实践中加深对 MyBa ...

  6. 手写 Vue 系列 之 Vue1.x

    前言 前面我们用 12 篇文章详细讲解了 Vue2 的框架源码.接下来我们就开始手写 Vue 系列,写一个自己的 Vue 框架,用最简单的代码实现 Vue 的核心功能,进一步理解 Vue 核心原理. ...

  7. 依据ECMA规范,手写一个bind函数

    Function.prototype.bind 函数,参见ECMA规范地址 如题,这次来实现一个boundFunction函数,不挂载在Function.prototype上,而是一个单独声明的函数. ...

  8. AI应用开发实战 - 手写识别应用入门

    AI应用开发实战 - 手写识别应用入门 手写体识别的应用已经非常流行了,如输入法,图片中的文字识别等.但对于大多数开发人员来说,如何实现这样的一个应用,还是会感觉无从下手.本文从简单的MNIST训练出 ...

  9. Caffe系列4——基于Caffe的MNIST数据集训练与测试(手把手教你使用Lenet识别手写字体)

    基于Caffe的MNIST数据集训练与测试 原创:转载请注明https://www.cnblogs.com/xiaoboge/p/10688926.html  摘要 在前面的博文中,我详细介绍了Caf ...

随机推荐

  1. storm学习路线指南

    关于对storm的介绍已经有很多了,我这里不做过多的介绍,我介绍一下我自己的学习路线,希望能帮助一些造轮子的同学走一些捷径,毕竟我也是站在前人总结整理的基础上学习了,如果有不足之处,还请大家不要喷我. ...

  2. markdown表情

    Emoji表情 将对应emoji表情的符号码复制后输入你的markdown文本即可显示emoji表情. 如:blush:,显示为

  3. 最全的Vue组件通信方式总结

    1.一图认清组件关系名词 父子关系:A与B.A与C.B与D.C与E 兄弟关系:B与C 隔代关系:A与D.A与E 非直系亲属:D与E 总结为三大类: 父子组件之间通信 兄弟组件之间通信 跨级通信 2.8 ...

  4. Java面向对象16种原则

    一   类的设计原则   1 依赖倒置原则-Dependency Inversion Principle (DIP) 2 里氏替换原则-Liskov Substitution Principle (L ...

  5. 用python twilio模块实现发手机短信的功能

    前排提示:这个模块不是用于对陌生人进行短信轰炸和电话骚扰的,这个模块也没有这个功能,如果是抱着这个心态来的,可以关闭网页了 语言:python 步骤一:安装twilio模块 pip install t ...

  6. js常用事件列表

    onmousedown.onmouseup 以及 onclick 事件 onmousedown, onmouseup 以及 onclick 构成了鼠标点击事件的所有部分.首先当点击鼠标按钮时,会触发 ...

  7. win10和浏览器快捷键

    1. Win10快捷键[Win+↑/↓/←/→] 将当前窗口按比例固定到屏幕的四个边角,如左上.右上.左下.右下.[Win+1/2/3…] 按顺序打开任务栏上的已固定程序(不包括第一个“任务视图”按钮 ...

  8. Linux(Ubuntu)安装Swift和Swiftlint

    很多时候iOS开发完毕需要接入CI中,而很多CI是基于Linux的,需要在Linux平台安装Swift和Swiftlint,下面就是针对这两个软件的安装步骤. Swift安装 环境 系统:Ubuntu ...

  9. android ——ListView

    谷歌官方文档的介绍:https://developer.android.com/reference/android/widget/ListView.html 显示可垂直滚动的视图集合,其中每个视图都立 ...

  10. 鲜为人知的maven标签解说

    目录 localRepository interactiveMode offline pluginGroups proxies servers mirrors profiles 使用场景 出现位置 激 ...