vue3?自定義圖片放大器效果的示例代碼
效果
具體代碼實(shí)現(xiàn)
創(chuàng)建商品圖片展示的vue頁(yè)面:ProductPicShow.vue
<script lang="ts" setup> import { ref, computed } from 'vue' import { useMouseInElement } from '@vueuse/core' defineProps<{ images: string[] }>() // 當(dāng)前顯示的圖片索引 let active = ref(0) // ref 獲取 DOM 元素的位置 const target = ref(null) // isisOutside為 true 的時(shí)候代表鼠標(biāo)未進(jìn)入目標(biāo)元素,為 false 時(shí)代表鼠標(biāo)進(jìn)入目標(biāo)元素 const { elementX, elementY, isOutside } = useMouseInElement(target) // 遮罩半透明圖在商品大圖中的坐標(biāo)位置 const position = computed(() => { let x = elementX.value - 100 let y = elementY.value - 100 if (x <= 0) x = 0 if (x >= 200) x = 200 if (y <= 0) y = 0 if (y >= 200) y = 200 return { x, y } }) </script> <template> <div class="product-image"> <!-- 放大鏡的大盒子 --> <div class="large" :style="[ { backgroundImage: `url(${images[active]})`, backgroundPosition: `-${position.x * 3}px -${position.y * 3}px` } ]" v-show="!isOutside" ></div> <div ref="target" class="middle"> <img :src="images[active]" alt="" /> <!-- 鼠標(biāo)移動(dòng)時(shí)的遮罩層 --> <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 scoped lang="less"> .product-image { width: 480px; height: 400px; position: relative; display: flex; z-index: 500; .large { position: absolute; top: 0; left: 412px; width: 600px; height: 600px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); background-repeat: no-repeat; background-size: 200% 200%; background-color: #f8f8f8; } .middle { width: 400px; height: 400px; background: #f5f5f5; position: relative; cursor: move; .layer { width: 200px; height: 200px; background: rgba(0, 0, 0, 0.2); left: 0; top: 0; position: absolute; } } .small { width: 80px; li { width: 68px; height: 68px; margin-left: 12px; margin-bottom: 15px; cursor: pointer; &:hover, &.active { border: 2px solid #27BA9B; } } } } </style>
使用:Product.vue
<template> <div class="product-info"> <div class="media"> <ProductPicShow :images="slidePics"/> </div> </div> </template> <script setup lang="ts"> import ProductPicShow from "@/views/product/components/ProductPicShow.vue" </script> <style scoped lang="less"> .product-info { min-height: 600px; background: #fff; display: flex; .media { width: 580px; height: 600px; padding: 30px 50px; } } </style>
到此這篇關(guān)于vue3 自定義圖片放大器的文章就介紹到這了,更多相關(guān)vue3 圖片放大器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3環(huán)境安裝以及項(xiàng)目搭建全過(guò)程
Vue工程化項(xiàng)目環(huán)境配置還是比較麻煩的,下面這篇文章主要給大家介紹了關(guān)于Vue3環(huán)境安裝以及項(xiàng)目搭建的相關(guān)資料,文中通過(guò)圖文以及代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12深入理解Vue響應(yīng)式原理及其實(shí)現(xiàn)方式
Vue的響應(yīng)式原理是Vue最核心的特性之一,也是Vue能夠?yàn)殚_(kāi)發(fā)者提供高效便捷的開(kāi)發(fā)體驗(yàn)的重要原因之一,這篇文章主要介紹了響應(yīng)式的原理及其實(shí)現(xiàn)方式,需要詳細(xì)了解可以參考下文2023-05-05深入探究Vue中$nextTick的實(shí)現(xiàn)原理
這篇文章主要為大家詳細(xì)介紹Vue中$nextTick的實(shí)現(xiàn)原理,文中的示例代碼講解詳細(xì),對(duì)我們深入了解Vue有一定的幫助,需要的小伙伴可以參考一下2023-06-06Vant?UI中van-collapse下拉折疊面板默認(rèn)展開(kāi)第一項(xiàng)的方法
之前做項(xiàng)目的時(shí)候,使用了Collapse折疊面板,下面這篇文章主要給大家介紹了關(guān)于Vant?UI中van-collapse下拉折疊面板默認(rèn)展開(kāi)第一項(xiàng)的相關(guān)資料,需要的朋友可以參考下2022-03-03Vue實(shí)現(xiàn)搜索 和新聞列表功能簡(jiǎn)單范例
本文通過(guò)實(shí)例代碼給大家介紹了Vue實(shí)現(xiàn)搜索 和新聞列表功能簡(jiǎn)單范例,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2018-03-03Vue3使用Proxy實(shí)現(xiàn)數(shù)據(jù)監(jiān)聽(tīng)的原因分析
在本篇文章里小編給大家整理的是一篇關(guān)于Vue3使用Proxy實(shí)現(xiàn)數(shù)據(jù)監(jiān)聽(tīng)的原因分析內(nèi)容,有需要的朋友們可以跟著學(xué)習(xí)參考下。2021-11-11vue?element?el-select下拉滾動(dòng)加載的方法
很多朋友都遇到這樣一個(gè)問(wèn)題在使用vue+element的el-select下拉框加載返回?cái)?shù)據(jù)太多時(shí),會(huì)造成卡頓,用戶體驗(yàn)欠佳,這篇文章主要介紹了vue?element?el-select下拉滾動(dòng)加載方法,需要的朋友可以參考下2022-11-11