vue3?自定義圖片放大器效果的示例代碼
效果

具體代碼實(shí)現(xiàn)
創(chuàng)建商品圖片展示的vue頁面: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 的時候代表鼠標(biāo)未進(jìn)入目標(biāo)元素,為 false 時代表鼠標(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)移動時的遮罩層 -->
<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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決vue項(xiàng)目Error:Cannot find module‘xxx’類報(bào)錯問題
當(dāng)npm運(yùn)行報(bào)錯Error:Cannot find module 'xxx'時,通常是因?yàn)閚ode_modules文件或依賴未正確安裝,解決步驟包括刪除node_modules和package-lock.json文件,重新運(yùn)行npm install,并根據(jù)需要安裝額外插件,若網(wǎng)絡(luò)問題導(dǎo)致安裝失敗2024-10-10
Vue Element-UI中el-table實(shí)現(xiàn)單選的示例代碼
在element-ui中是為我們準(zhǔn)備好了可直接使用的單選與多選屬性的,本文主要介紹了Vue Element-UI中el-table實(shí)現(xiàn)單選的示例代碼,具有一定的參考價值,感興趣的可以了解一下2023-12-12
Vue 3 + Element Plus 封裝單列控制編輯的可編輯表格
在Web應(yīng)用開發(fā)中實(shí)現(xiàn)表格數(shù)據(jù)編輯功能至關(guān)重要,本文將詳細(xì)介紹如何使用Vue3和ElementPlus庫來構(gòu)建一個支持單列編輯的表格組件,本教程詳細(xì)闡述了組件創(chuàng)建過程和數(shù)據(jù)綁定機(jī)制,幫助你快速掌握Vue3中表格編輯功能的實(shí)現(xiàn),感興趣的朋友一起看看吧2024-09-09
Vue使用extend動態(tài)創(chuàng)建組件的實(shí)現(xiàn)
本文主要介紹了Vue使用extend動態(tài)創(chuàng)建組件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
vue watch內(nèi)部調(diào)用methods方法報(bào)錯的解決方案
這篇文章主要介紹了vue watch內(nèi)部調(diào)用methods方法報(bào)錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04

