一个手写的Vue放大镜,复制即可使用
程序员文章站
2023-11-10 20:37:10
一个手写的vue放大镜 组件使用less,请确保已安装loader 本组件为放大镜组件,传参列表为: width: 必传,设置放大镜的宽高(正方形),放大区域等同,放大倍数为2倍 picList:必传,传入图片列表 使用示例: script: import mirror from 'xx/mirro ......
一个手写的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
效果
下一篇: HTML回顾