vue3实现商品图片放大镜效果(芋道源码yudao-cloud 二开笔记)

今天开发一个防某商城的商品图片放大镜,鼠标移动到图片位置时,右侧出现一个已放大的图片效果。

示例如下:

下图的图片的放大效果和小图的切换封装成了组件PicShow.vue,可根据需求自行修改,如下:

第一步:组件代码使用
javascript 复制代码
<template>
	<div>
		<!-- 大图、轮播图、放大镜 -->
	  	<PicShow :images="imgList"/>
	</div>
</template>
<script setup lang="ts">
const state = ref([
	'https://static.iocoder.cn/mall/a79f5d2ea6bf0c3c11b2127332dfe2df.jpg',
	'https://static.iocoder.cn/mall/a79f5d2ea6bf0c3c11b2127332dfe2df.jpg',
	'https://static.iocoder.cn/mall/a79f5d2ea6bf0c3c11b2127332dfe2df.jpg',
	'https://static.iocoder.cn/mall/a79f5d2ea6bf0c3c11b2127332dfe2df.jpg'
])
</script >
第二步: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 >= 175) x = 175
    if (y <= 0) y = 0
    if (y >= 175) y = 175
    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: 360px;
    width: 500px;
    height: 500px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    background-size: 170% 170%;
    background-color: #f8f8f8;
  }
  .middle {
    width: 350px;
    height: 350px;
    background: #f5f5f5;
    position: relative;
    cursor: move;
    .layer {
      width: 175px;
      height: 175px;
      background: rgba(0, 0, 0, 0.2);
      left: 0;
      top: 0;
      position: absolute;
    }
    img{
      width: 350px;
      height: 350px;
    }
  }
  .small {
    width: 342px;
    display: flex;
    margin-top: 5px;
    padding: 8px 8px;
    overflow-x: auto;
    margin-left: -8px;
    li {
      width: 64px;
      height: 64px;
      cursor: pointer;
      list-style: none;
      box-shadow: 0px 0px 8px #ccc;
      margin-left: 5px;
      img{
        width: 64px;
        height: 64px;
      }
    }
  }
  ul{
    margin: 0;
  }
  ul li:first-child{
    margin-left: 0 !important;
  }
}
</style>
ok了。。。。。。
相关推荐
北城笑笑1 小时前
Vue 115 ,上篇:给智能体装上计费表,行知订阅次数额度与每日额度双规则设计( 需求分析与方案设计,Vue3 + RuoYi 3.9.2 + 主子账号 )
java·前端·javascript·数据库·vue.js·后端·vue
GuWenyue1 小时前
订单号末尾变成了 0,对账还少了 1 分钱:一个 53 位尾数惹的祸
javascript·数据库·后端
小y的笔记1 小时前
React useReducer 实战:复杂组件状态转换与可测试的业务流程
前端·javascript
豌豆学姐1 小时前
图片数字人API接入教程:通过人物图片和音频实现AI口型同步视频生成
java·前端·javascript
是菜鸡小小陈啊2 小时前
【CS329Z Assignment 1】从零构建 LLM Agent Harness(学习笔记)06
人工智能·笔记·python·学习·llm·agent
摇滚侠2 小时前
《面包板电子制作 130 例》阅读笔记 4
笔记·单片机·嵌入式硬件
只睡四小时2 小时前
LoAF 归因:原生 JS 每次输入 201 次布局读取
开发语言·javascript·性能优化·ecmascript·甘特图·前端性能·loaf
派大猩猩_2 小时前
Obsidian U盘里的笔记库怎么安家?2026 随身库转云端主库
笔记·obsidian·obsidian插件·obsidian同步·obsidian同步插件
柳鲲鹏3 小时前
梦笔记20291203
笔记
欢欢李3 小时前
【LTE L3 笔记 53】LTE下行动态功率控制
笔记·无线通信·物理层·lte·空口协议