当前位置: 首页 > news >正文

Vue3 实现产品图片放大器

Vue3 实现类似淘宝、京东产品详情图片放大器功能
环境:vue3+ts+vite

1.创建picShow.vue组件

<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 - 70let y = elementY.value - 70if (x <= 0) x = 0if (x >= 140) x = 140if (y <= 0) y = 0if (y >= 140) y = 140return {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

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

效果:
在这里插入图片描述

http://www.lryc.cn/news/112938.html

相关文章:

  • 机器学习笔记 - 使用 YOLOv5、O​​penCV、Python 和 C++ 检测物体
  • 使用手机相机检测电脑屏幕刷新率Hz
  • 修改k8s pod的挂载目录
  • Vue3+SpringBoot快速开发模板
  • CSDN竞赛68期
  • Redis入门
  • [CrackMe]BuLLeT.exe的逆向及注册机编写
  • C++ 中 int、short、long和long long 分别是几位?有符号无符号有什么区别?
  • Killing LeetCode [82] 删除排序链表中的重复元素 II
  • LeetCode 热题 100 JavaScript--283. 移动零
  • java读写ini文件
  • 【ARM Coresight 系列文章 2.3 - Coresight 寄存器】
  • kafka:java client使用总结塈seek() VS commitSync()的区别(三)
  • 如何用正确的姿势监听Android屏幕旋转
  • mysql高级三:sql性能优化+索引优化+慢查询日志
  • HCIP VLAN--Hybrid接口
  • 大数据开发面试必问:Hive调优技巧系列二
  • 【C++】STL——list的模拟实现、构造函数、迭代器类的实现、运算符重载、增删查改
  • vscode 插件::EIDE
  • Python 网络编程
  • SQL 数据科学:了解和利用联接
  • (统计学习方法|李航)第五章决策树——四五节:决策树的剪枝,CART算法
  • C语言--结构体定义
  • 解决Element Plus中Select在El Dialog里层级过低的问题(修改select选项框样式)
  • 【数据结构】二叉树 链式结构的相关问题
  • 【无标题】云原生在工业互联网的落地及好处!
  • 人工智能在心电信号分类中的应用
  • 【Linux 网络】网络层协议之IP协议
  • .meta 文件
  • CRITICAL_SECTION 用法