一個(gè)手寫的vue放大鏡效果
組件使用less,請(qǐng)確保已安裝loader
本組件為放大鏡組件,傳參列表為:
•width: 必傳,設(shè)置放大鏡的寬高(正方形),放大區(qū)域等同,放大倍數(shù)為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" />
詳細(xì)代碼:
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){ //計(jì)算元素到body的絕對(duì)位置 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 } //計(jì)算比例 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>
可直接復(fù)制文件內(nèi)容至項(xiàng)目使用,文件地址:https://blog-static.cnblogs.com/files/hhyf/mirror.vue.js
效果
總結(jié)
以上所述是小編給大家介紹的一個(gè)手寫的vue放大鏡效果,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
Vue.js實(shí)戰(zhàn)之組件之間的數(shù)據(jù)傳遞
這篇文章主要介紹了Vue.js實(shí)戰(zhàn)之組件之間的數(shù)據(jù)傳遞的相關(guān)資料,文中通過示例代碼和圖文介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-04-04vue實(shí)現(xiàn)右鍵點(diǎn)擊彈框信息功能
這篇文章主要介紹了vue實(shí)現(xiàn)右鍵點(diǎn)擊彈框信息功能方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12vant自定義引入iconfont圖標(biāo)及字體的方法步驟
因?yàn)関antUI給的圖標(biāo)非常少,為了滿足自己的需求,就應(yīng)該找到一種方法來向vant添加自己自定義的圖標(biāo),對(duì)于自定義圖標(biāo)我第一時(shí)間想到的就是阿里的iconfont矢量圖庫,這篇文章主要給大家介紹了關(guān)于vant自定義引入iconfont圖標(biāo)及字體的方法步驟,需要的朋友可以參考下2023-09-09Vue動(dòng)態(tài)加載圖片在跨域時(shí)無法顯示的問題及解決方法
這篇文章主要介紹了解決VUE動(dòng)態(tài)加載圖片在跨域時(shí)無法顯示的問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03Vue2.0 給Tab標(biāo)簽頁和頁面切換過渡添加樣式的方法
下面小編就為大家分享一篇Vue2.0 給Tab標(biāo)簽頁和頁面切換過渡添加樣式的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03Vue使用fabric.js實(shí)現(xiàn)局部截圖與大圖預(yù)覽功能
這篇文章主要為大家詳細(xì)介紹了Vue如何使用fabric.js實(shí)現(xiàn)局部截圖與el-image-viewer大圖預(yù)覽功能,文中的示例代碼講解詳細(xì),感興趣的可以了解下2024-02-02