您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“vue如何實現放大鏡效果”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“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" : > <div ref="truth" : @mousemove="move" @mouseenter="showMagnify" @mouseleave="hideMagnify"> <div class="mask" ref="mask" v-show = "showMask" :></div> <img :src="picList[picIndex]" draggable="false"/> </div> <div class="virtual" ref="virtual" v-if = "isShowVirtual" : > <div class="big" ref = "bigPic" : > </div> </div> </div> <ul class="picList" :> <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>
以上是“vue如何實現放大鏡效果”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。