一个手写的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. Linux小火车和流星雨

    一.流星雨 [root@localhost ~]# wget https://jaist.dl.sourceforge.net/project/cmatrix/cmatrix/1.2a/cmatrix ...

  2. 算法与数据结构基础 - 广度优先搜索(BFS)

    BFS基础 广度优先搜索(Breadth First Search)用于按离始节点距离.由近到远渐次访问图的节点,可视化BFS 通常使用队列(queue)结构模拟BFS过程,关于queue见:算法与数 ...

  3. selenium Java中常见等待的几种形式

    前言 在自动化测试中,我们经常会碰到编写脚本过程中操作某个元素的时候, 需要等待页面加载完成后,才能对元素操作,否则会报错,提示页面元素不存在异常,我们需要等待元素加载完成后,才能继续操作,而Sele ...

  4. 用JSP从数据库中读取图片并显示在网页上

    <1>先在mysql下建立如下的table. 并insert图像. mysql.sql文件如下: CREATE TABLE photo ( photo_no int(6) unsigned ...

  5. 【Intellij】导出 jar 包

    需要在 Intellij 导出 jar 包,一时不知道该怎么做,后来总算找到了方法,步骤如下: 1. File → Project Structure... → Artifacts → + → jar ...

  6. PID算法资料【视频+PDF介绍】

    最近一直有网友看到我的博客后,加我好友,问我能不能给发一些PID的资料,今天找了一些资料放到百度网盘上,给大家下载: 视频资料 链接:https://pan.baidu.com/s/12_IlLgBI ...

  7. hdu 6397 Character Encoding (生成函数)

    Problem Description In computer science, a character is a letter, a digit, a punctuation mark or som ...

  8. dubbo异常处理

    dubbo异常处理 我们的项目使用了dubbo进行不同系统之间的调用. 每个项目都有一个全局的异常处理,对于业务异常,我们会抛出自定义的业务异常(继承RuntimeException). 全局的异常处 ...

  9. jmeter使用JDBC连接数据库

    jmeter使用JDBC的配置元件连接数据库,通过sql语句查询需用到的数据 配置元件名称:JDBC connection configuration,使用前,需导入mysql-connector-j ...

  10. 1、JAVA的小白之路

    大学的时光过得很快,转眼我已经大二了,在大一时,学习了C\C++,对于语言有一定基础,在未来的道路上,我需要攒足干劲,积累足够的知识和技能,去走上社会. 我的第一任大学班主任告诉我:“作为程序员,你至 ...