Vue实现网页放大镜效果

更新日期: 2019-08-09阅读: 2.8k标签: 效果

组件使用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"  :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){ //计算元素到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


效果


来自:https://www.cnblogs.com/hhyf/archive/2019/08/09/11326389.html



链接: https://www.fly63.com/article/detial/4590

使用 JavaScript 实现分屏视觉效果

今天这篇文章就来讲讲使用JavaScript来实现这种分屏的视觉UI效果。现在在网站上这种分屏视觉效果应用的也非常广泛,比如 Corsair website。

CSS3的过渡效果,使用transition实现鼠标移入/移出效果

在css中使用伪类虽然实现了样式的改变,但由于没有过渡效果会显得很生硬。以前如果要实现过渡,就需要借助第三方的js框架来实现。现在只需要使用CSS3的过渡(transition)功能,就可以从一组样式平滑的切换到另一组样式。

js如何实现新手引导效果?

js最近有个小伙伴问到了怎么实现新手引导的效果,然后便去网上找了下实现方案。可以通过css的border来实现。

css3 斜切角/斜边的实现方式

设计图含有斜切角的效果时,我们一般想到的方法是切出四个角为背景,然后用border连起来,这样就能显示出该效果了,那么直接使用css呢?下面就整理css做斜边的效果。

JavaScript 实现打字机效果,跑马灯效果

这篇文章在不使用任何插件的情况,以最简洁的原生javascript来实现打字机效果和跑马灯效果。打字效果即把一段话一个字一个字的显示出来。

CSS遮罩效果(模糊效果,阴影效果,毛玻璃效果)

一般遮罩加上透明度opacity就是阴影效果。阴影效果和一般遮罩一样,唯一不同的是设置.mask遮罩的背景色用rgba()表示,当然hsla()也是可以的。模糊效果(毛玻璃效果) 通过 filter来实现

纯css实现气泡效果

主要运用的是1.border 组成的直角三角形。2,before 和 after 伪元素 。3,z-index属性;将元素的长宽设置为0,并且将border的3条边设置为透明的,就会出现border颜色的直角三角形

css文字选中效果

文字选中效果,这个可能很少有人注意过。在默认状态先一般选中的文本颜色是白字蓝底的,不过可以通过CSS进行设置。::selection定义元素上的伪选择器,以便在选定元素时设置其中文本的样式。

text-fill-color:仿苹果官网介绍效果 CSS设置文字渐变效果 文字背景图遮罩

发布iPhone XR的时候 各种心动 去官网看了一遍又一遍。闲着无聊发现 里面的介绍很用大篇幅的有背景文字来介绍。Like this:看着挺酷炫的还不错 就看了下实现方式。还挺简单的。

Vue 中多个元素、组件的过渡,及列表过渡

多元素之间如何实现过渡动画效果呢?这么写行不行呢?肯定是不行的,因为 Vue 在两个元素进行切换的时候,会尽量复用dom,就是因为这个原因,导致现在动画效果不会出现。如果不让 Vue 复用dom的话,应该怎么做呢?只需要给这两个div不同的key值就行了

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!