国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

一個(gè)手寫的vue放大鏡效果

2024-05-06 15:36:01
字體:
供稿:網(wǎng)友

組件使用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)    }  }  }            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 祁门县| 霍山县| 柳林县| 民权县| 通渭县| 蒙阴县| 南木林县| 清徐县| 东宁县| 平湖市| 沛县| 广水市| 彝良县| 广元市| 乌苏市| 汶川县| 凤冈县| 赣榆县| 简阳市| 桦甸市| 桂林市| 高密市| 福海县| 宜宾县| 三穗县| 新疆| 三亚市| 玉溪市| 界首市| 巴东县| 同德县| 桓台县| 茌平县| 奉新县| 荔波县| 图们市| 舞钢市| 沙雅县| 抚州市| 炎陵县| 贡觉县|