vue3 电商常见的图片预览功能

218 阅读1分钟
<template>
  <div class="goods-image">
    <div
      class="large"
      v-show="isShow"
      :style="{
        backgroundImage: `url(${imageList[imgIndex]})`,
        backgroundPositionX: positionX + 'px',
        backgroundPositionY: positionY + 'PX'
      }"
    ></div>
    <div class="middle" ref="target">
      <!-- 通过获取点击小图的下标来显示大图 -->
      <img :src="imageList[imgIndex]" alt="" />
      <div
        class="layer"
        v-show="isShow"
        :style="{ left: left + 'px', top: top + 'px' }"
      ></div>
    </div>
    <div class="small">
      <ul>
        <!-- 大图显示的图片的下标与小图的下标一致,显示小图的边框 -->
        <li
          v-for="(item, index) in imageList"
          @mouseenter="getImgIndex(index)"
          :key="index"
          :class="{ active: imgIndex === index }"
        >
          <img :src="item" alt="" />
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { ref, watch } from 'vue'
import { useMouseInElement } from '@vueuse/core'

export default {
  name: 'XtxImageView',
  props: {
    imageList: {
      type: Array,
      default: () => []
    }
  },
  setup (props) {
    // console.log(props.imageList, 9999)
    const imgIndex = ref(0)
    // 实现放大镜效果
    const target = ref(null)
    // 控制预览图和遮罩层的显示和隐藏
    const isShow = ref(false)
    const left = ref(0)
    const top = ref(0)
    const positionX = ref(0)
    const positionY = ref(0)
    // elementX 盒子距离左侧的距离  elementY 盒子距离上的距离  isOutside 鼠标是否在图片外面 true就是在图片外面
    const { elementX, elementY, isOutside } = useMouseInElement(target)
    //  鼠标点击小图 获取小图下标
    const getImgIndex = (index) => {
      imgIndex.value = index
    }
    // 监听坐标值改变,改变预览图和遮罩层的显示和隐藏
    watch([elementX, elementY, isOutside], () => {
      isShow.value = !isOutside.value
      // 鼠标在图片外面 就不要计算
      if (isOutside.value) {
        return false
      }
      // x轴方法坐标范围的控制
      // 左侧
      if (elementX.value > 300) {
        left.value = 200
      }
      // 右侧
      if (elementX.value < 100) {
        left.value = 0
      }
      // 中间
      if (elementX.value < 300 && elementX.value > 100) {
        left.value = elementX.value - 100
      }
      // y轴方向坐标范围的控制
      // 右侧
      if (elementY.value > 300) {
        top.value = 200
      }
      // 左侧
      if (elementY.value < 100) {
        top.value = 0
      }
      // 中间
      if (elementY.value < 300 && elementY.value > 100) {
        top.value = elementY.value - 100
      }
      // 预览大图的移动的距离
      positionX.value = -left.value * 2
      positionY.value = -top.value * 2
    })
    console.log(elementX, elementY, isOutside, 5555)
    return {
      imgIndex,
      getImgIndex,
      target,
      elementX,
      elementY,
      isOutside,
      isShow,
      left,
      top,
      positionX,
      positionY
    }
  }
}
</script>

<style scoped lang="less">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
  }
  .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;
    // 背景图:盒子的大小 = 2:1  将来控制背景图的移动来实现放大的效果查看 background-position
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }
  .layer {
    width: 200px;
    height: 200px;
    background: rgba(0, 0, 0, 0.2);
    // 绝对定位 然后跟随咱们鼠标控制lefttop属性就可以让滑块移动起来
    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>