DDR爱好者之家 Design By 杰米
组件使用less,请确保已安装loader
本组件为放大镜组件,传参列表为:
"htmlcode">
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="/UploadFiles/2021-04-02/picList[picIndex]">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放大镜效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
更新日志
2024年03月29日
2024年03月29日
- 苏有朋.1994-珍惜的背包(第3张)【飞碟】【WAV+CUE】
- 《宝可梦:朱紫》“蓝之圆盘”IGN7分:性能优化仍旧灾难
- 《塞尔达传说:王国之泪》大获成功!多项年度数据表现优秀
- 应景!欧任用《塞尔达传说:王国之泪》堆雪人
- 杨采妮.1996-为所欲为(国专)【EMI百代】【WAV+CUE】
- 黎姿.1993-如果真的恋爱【华纳】【WAV+CUE】
- 朱婧汐.2024-VILLAIN反派角色【StreetVoic】【FLAC分轨】
- 《喷射战士3》最新祭典公布 1月13日举办
- NS日服年度下载榜公开:《王泪》不敌《西瓜游戏》
- 科乐美《心跳回忆GS》系列推出复刻版 2月14日登陆NS
- 林峰.2012-SELF-PORTRAIT【英皇娱乐】【WAV+CUE】
- TENSION.2004-Story新歌+精丫EMI百代】【FLAC分轨】
- 草蜢.2000-环球2000超巨星系列【环球】【WAV+CUE】
- 国行Switch开启元旦折扣!《马里奥奥德赛》7折促销
- 《塞尔达》官方账号感谢玩家支持 发布王泪最新壁纸