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了。。。。。。
相关推荐
_ziva_几秒前
Miniconda 下载 + 安装 + VS Code 集成使用教程
笔记
tq108610 分钟前
商业环境中的三重生命:自然、职业与组织的平衡模型
笔记
灏瀚星空25 分钟前
基于 Python 与 GitHub,打造个人专属本地化思维导图工具全流程方案(上)
开发语言·人工智能·经验分享·笔记·python·个人开发·visual studio
资源存储库26 分钟前
【笔记】如何修改一个conda环境的python版本?
笔记·python·conda
qq_3975623137 分钟前
昆仑通态, ModbusTCP数据转发, 驱动,使用笔记
笔记
阿豪只会阿巴1 小时前
【多喝热水系列】从零开始的ROS2之旅——Day9 初识话题通信:基本命令
c++·笔记·python·ubuntu·ros2
崎岖Qiu1 小时前
【OS笔记44】:磁盘存储管理
笔记·操作系统·os
周周记笔记1 小时前
ESP32-S3 :开发方式笔记(五)
笔记·单片机·嵌入式硬件
June bug1 小时前
【实习笔记】Fiddler学习笔记
笔记·学习·fiddler
googleccsdn1 小时前
ENSP Pro Lab笔记:配置BGP EVPN VXLAN双栈(2)
网络·笔记·网络协议