放大镜效果

插件下载

npm install @vueuse/core # yarn add @vueuse/core

封装独立组件

<template>

  <div class="goods-image">

    <div

      class="large"

      :style="[

        { backgroundImage: `url(${images[currIndex]})` },

        { backgroundPosition: `${backgroundposition.left} ${backgroundposition.top}` }

      ]"

      v-if="!hide"

    ></div>

    <ul class="small">

      <li v-for="(img, i) in images" :key="img" :class="{ active: i === currIndex }">

        <img @mouseenter="currIndex = i" :src="img" alt="" />

      </li>

    </ul>

    <div class="middle" ref="target" @mouseenter="hide = false" @mouseleave="hide = true">

      <img :src="images[currIndex]" alt="" />

      <div class="layer" :style="position"></div>

    </div>

   

  </div>

</template>

<script>

import { ref, reactive, watch } from 'vue'

import { useMouseInElement } from '@vueuse/core'

export default {

  props: {

    images: {

      type: Array,

      default: () => []

    }

  },

  setup(props) {

   

    const currIndex = ref(0)

    const hide = ref(true)

    const target = ref(null)

    // 遮罩层位置坐标

    const position = reactive({

      left: 0,

      top: 0

    })

    const backgroundposition = reactive({

      left: 0,

      top: 0

    })

    const { elementX, elementY, isOutside } = useMouseInElement(target)

    watch([elementX, elementY, isOutside], v1 => {

      if (isOutside.value) return

      // X方向坐标范围控制

      if (elementX.value < 100) {

        // 左侧

        position.left = 0

      } else if (elementX.value > 300) {

        // 右侧

        position.left = 200

      } else {

        // 中间

        position.left = elementX.value - 100

      }

      // Y方向坐标范围控制

      if (elementY.value < 100) {

        // 左侧

        position.top = 0

      } else if (elementY.value > 300) {

        // 右侧

        position.top = 200

      } else {

        // 中间

        position.top = elementY.value - 100

      }

      // 计算预览大图的移动的距离

      backgroundposition.left = -position.left * 2 + 'px'

      backgroundposition.top = -position.top * 2 + 'px'

      // 计算遮罩层的位置

      position.left = position.left + 'px'

      position.top = position.top + 'px'

      // 方法2

      // if (!v1[2]) {

      //   console.log(v1)

      //   if (v1[0] <= 300) {

      //     position.left = v1[0] - 100 + 'px'

      //     backgroundposition.left = -(v1[0] - 100) * 2 + 'px'

      //     if (v1[0] <= 100) {

      //       position.left = 0 + 'px'

      //       backgroundposition.left = 0 + 'px'

      //     }

      //   } else if (v1[0] > 300) {

      //     position.left = 200 + 'px'

      //   }

      //   if (v1[1] <= 300) {

      //     position.top = v1[1] - 100 + 'px'

      //     backgroundposition.top = -(v1[1] - 100) * 2 + 'px'

      //     if (v1[1] <= 100) {

      //       position.top = 0 + 'px'

      //       backgroundposition.top = 0 + 'px'

      //     }

      //   } else if (v1[1] > 300) {

      //     position.top = 200 + 'px'

      //   }

      // }

    })

    return { currIndex, target, position, backgroundposition, hide }

  }

}

</script>

<style lang="less" scoped>

.goods-image {

  width: 650px;

  height: 400px;

  position: relative;

  display: flex;

  z-index: 500;

  .large {

    position: absolute;

    top: 0;

    left: 412px;

    width: 400px;

    height: 400px;

    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);

    background-repeat: no-repeat;

    background-size: 800px 800px;

    background-color: #f8f8f8;

  }

  .middle {

    width: 400px;

    height: 400px;

    margin-left: 20px;

    background: #f5f5f5;

    img{

      width: 100%;

      height: 100%;

    }

  }

  .small {

    width: 80px;

    li {

      width: 68px;

      height: 68px;

      margin-left: 12px;

      margin-bottom: 15px;

      img{

        width: 100%;

      height: 100%;

      }

      cursor: pointer;

      &:hover,

      &.active {

        border: 2px solid gray;

      }

    }

  }

}

.middle:hover {

  position: relative;

  cursor: move;

  .layer {

    width: 200px;

    height: 200px;

    background: rgba(0, 0, 0, 0.2);

    left: 0;

    top: 0;

    position: absolute;

  }

}

</style>

使用方法

1.导入组件文件地址

2.注册组件

components:{ GoodsImage }

3.使用

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值