vue3图片放大镜效果

使用vue3的核心库,获得鼠标位置,进行左右移动并且有放大效果

<template>
  <div class="goods-image">

    <div v-if="!isOutside" class="large" :style="{backgroundImage:`url(${imageList[curIndex]})`,
    backgroundPositionX: positionX + 'px',
    backgroundPositionY: positionY + 'px'
    }"></div>

    <div class="middle" ref="target" >
      <img :src="imageList[curIndex]" alt="">
       <!-- 蒙层容器 -->
     <div class="layer" :style="{left:left+'px',top:top+'px'}" v-if="!isOutside"></div>
    </div>
    <ul class="small">
      <li v-for="(img,i) in imageList" :key="i" @mouseenter="mouseenter(i)" :class="{active:curIndex===i}">
        <img :src="img"  alt="">
      </li>
    </ul>
  </div>
</template>
<script>
import { ref, watch } from 'vue'
import { useMouseInElement } from '@vueuse/core'
export default {
  name: 'GoodsImage',
  props: {
    imageList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  setup () {
    const curIndex = ref(0)
    function mouseenter (index) {
      curIndex.value = index
    }
    const target = ref(null)

    const { elementX, elementY, isOutside } = useMouseInElement(target)
    // 监听
    const left = ref(0)
    const top = ref(0)
    // 定位位置
    const positionX = ref(0)
    const positionY = ref(0)
    watch([elementX, elementY], () => {
      // 监听处理
      // 移动
      if (elementX.value < 100) {
        left.value = 0
      }
      if (elementX.value > 300) {
        left.value = 200
      }
      if (elementY.value < 100) {
        top.value = 0
      }
      if (elementY.value > 300) {
        top.value = 200
      }
      // 有效范围
      if (elementX.value > 100 && elementX.value < 300) {
        left.value = elementX.value - 100
      }
      if (elementY.value > 100 && elementY.value < 300) {
        top.value = elementY.value - 100
      }
      // 控制背景图:1.方向相反  2.比例1:2或者更大
      positionX.value = -left.value * 2
      positionY.value = -top.value * 2
    })
    return {
      curIndex,
      mouseenter,
      target,
      left,
      top,
      isOutside,
      positionX,
      positionY
    }
  }
}
</script>
<style scoped lang="less">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  // 大图盒子
   .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    z-index:500;
    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;
    background: #f5f5f5;
  }
  // 蒙层
  .layer {
  width: 200px;
  height: 200px;
  background: rgba(0, 0, 0, 0.2);
  left: 0;
  top: 0;
  position: absolute;
}
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,&.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
</style>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值