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',
]

效果:

相关推荐
FungLeo28 分钟前
成为全栈·React 管理后台篇·总复盘:从“接口能调通”到“后台值得使用”
前端·react.js·前端框架·项目复盘·成为全栈
Csvn2 小时前
并发模式:让渲染学会排队、插队和让路
前端
可乐鸡翅yeah_2 小时前
新手梳理:M3U8 线上问题,哪些是前端锅,哪些是后端锅
前端·ios·音视频·实时音视频·m3u8·音视频在线播放
Flynt2 小时前
Linear 用 1000 个 PR 换掉 styled-components,我写了 200 个按钮,把这笔账复现了一遍
前端·css·preact
JavaGuide3 小时前
NVIDIA 又开源了!这次给 AI Agent 加上权限管控
前端·后端
excel3 小时前
prisma 如何处理数据库竞态
前端·数据库·后端
莪_幻尘5 小时前
Skill 体检:30 个 Skill 全凭感觉?体检器先自曝了 8 个“假 0 分
前端·人工智能·llm
风骏时光牛马5 小时前
AI模型综合能力评测:性能、指令遵循与多场景实测对比
前端
Frag0ut5 小时前
Chrome与Chromium内核浏览器在Windows 11上的新特性全景解析
前端·chrome·windows·web安全·chromium·gemini ai·playready drm
hiahiahia1235 小时前
实现完整 Tool Dispatcher
开发语言·前端