Vue3 实现产品图片放大器

Vue3 实现类似淘宝、京东产品详情图片放大器功能

环境:vue3+ts+vite

1.创建picShow.vue组件

javascript 复制代码
<script lang="ts" setup>
import {ref, computed} from 'vue'
import {useMouseInElement} from '@vueuse/core'

/*获取父组件的传值*/
defineProps<{
    images: string[]
}>()
// 当前显示的图片索引
let active = ref(0)
// ref 获取 DOM 元素的位置
const target = ref(null)
// isisOutside为 true 的时候代表鼠标未进入目标元素,为 false 时代表鼠标进入目标元素
const {elementX, elementY, isOutside} = useMouseInElement(target)
// 遮罩半透明图在商品大图中的坐标位置
const position = computed(() => {
    let x = elementX.value - 70
    let y = elementY.value - 70
    if (x <= 0) x = 0
    if (x >= 140) x = 140
    if (y <= 0) y = 0
    if (y >= 140) y = 140
    return {x, y}
})
</script>
<template>
    <div class="product-image">
        <!-- 放大 -->
        <div class="large" :style="[{ backgroundImage: `url(${images[active]})`,
          backgroundPosition: `-${position.x * 2}px -${position.y * 2}px`
        }]" v-show="!isOutside"></div>
        <div ref="target" class="middle">
            <img :src="images[active]" alt=""/>
            <!-- 遮罩层 -->
            <div class="layer" v-show="!isOutside" :style="{ left: `${position.x}px`, top: `${position.y}px` }"></div>
        </div>
        <ul class="small">
            <li v-for="(item, index) in images" :key="item"
                    :class="{ active: index === active }"
                    @mouseenter="active = index">
                <img :src="item" alt=""/>
            </li>
        </ul>
    </div>
</template>

<style lang="scss" scoped>
.product-image {
  position: relative;
  z-index: 500;
  .large {
    position: absolute;
    top: 0;
    left: 290px;
    width: 500px;
    height: 500px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    background-size: 156% 156%;
    background-color: #f8f8f8;
  }
  .middle {
    width: 280px;
    height: 280px;
    background: #f5f5f5;
    position: relative;
    cursor: move;
    .layer {
      width: 140px;
      height: 140px;
      background: rgba(0, 0, 0, 0.2);
      left: 0;
      top: 0;
      position: absolute;
    }
    img{
      width: 280px;
      height: 280px;
    }
  }
  .small {
    width: 280px;
    display: flex;
    margin-top: 5px;
    li {
      width: 70px;
      height: 70px;
      cursor: pointer;
      list-style: none;
      img{
        width: 70px;
        height: 70px;
      }
    }
  }
}
</style>

2.在其他页面引用组件picShow.vue

javascript 复制代码
<div class="product-info">
   <div class="media">
        <PicShow :images="picList"/>
    </div>
</div>

//js部分
import PicShow from "@/components/picShow.vue";
const picList=[
    '/src/assets/images/contactus01.jpg',
    '/src/assets/images/contactus02.jpg',
    '/src/assets/images/contactus03.jpg',
    '/src/assets/images/contactus04.jpg',
]

效果:

相关推荐
崔庆才丨静觅7 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby60617 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了8 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅8 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅8 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅8 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment8 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅9 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊9 小时前
jwt介绍
前端
爱敲代码的小鱼9 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax