vue如何实现放大镜效果-创新互联

这篇文章主要为大家展示了“vue如何实现放大镜效果”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“vue如何实现放大镜效果”这篇文章吧。

10年积累的网站制作、成都网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有三门峡免费网站建设让你可以放心的选择与我们合作。

组件使用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如何实现放大镜效果”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联成都网站设计公司行业资讯频道!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

网站栏目:vue如何实现放大镜效果-创新互联
文章出自:https://www.cdcxhl.com/article36/esipg.html

成都网站建设公司_创新互联,为您提供网站改版网页设计公司做网站小程序开发外贸建站网站营销

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

h5响应式网站建设