Vue3實(shí)現(xiàn)圖片放大鏡效果
本文實(shí)例為大家分享了Vue3實(shí)現(xiàn)圖片放大鏡效果的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)效果

代碼
<template>
<div class="goods-image">
<!-- 大圖容器 -->
<div
class="large"
:style="[
{
backgroundImage: `url(${imageList[curId]})`,
backgroundPositionX: position.backgroundPositionX,
backgroundPositionY: position.backgroundPositionY,
},
]"
v-if="isShow"
></div>
<div class="middle" ref="target">
<img :src="imageList[curId]" alt="" />
<!-- 蒙層容器 -->
<div class="layer" :style="{ left: left + 'px', top: top + 'px' }" v-if="isShow"></div>
</div>
<ul class="small">
<li
v-for="(img, i) in imageList"
:key="i"
@mouseenter="curId = i"
:class="{ active: curId === i }"
>
<img :src="img" alt="" />
</li>
</ul>
</div>
</template>
<script>
import { reactive, ref, watch } from 'vue'
import { useMouseInElement } from '@vueuse/core'
export default {
name: 'GoodsImage',
props: {
imageList: {
type: Array,
default: () => {
return []
}
}
},
setup () {
const curId = ref(0)
const target = ref(null)
// elementX: 鼠標(biāo)距離左側(cè)的偏移值
// elementY:表表距離頂部的偏移值
// isOutside: 是否在容器外部 外部true 內(nèi)部 false
const { elementX, elementY, isOutside } = useMouseInElement(target)
const left = ref(0) // 滑塊距離左側(cè)的距離
const top = ref(0) // 滑塊距離頂部的距離
const isShow = ref(false) // 顯示大圖和蒙層圖的顯示和隱藏
const position = reactive({ // 大圖顯示的位置,默認(rèn)是0
backgroundPositionX: 0,
backgroundPositionY: 0
})
watch(
// 監(jiān)聽的對(duì)象
[elementX, elementY, isOutside],
() => {
if (elementX.value < 100) {
// 左側(cè)最小距離
left.value = 0
}
if (elementX.value > 300) {
left.value = 200
}
if (elementX.value > 100 && elementX.value < 300) {
left.value = elementX.value - 100
}
if (elementY.value < 100) {
// 左側(cè)最小距離
top.value = 0
}
if (elementY.value > 300) {
top.value = 200
}
if (elementY.value > 100 && elementY.value < 300) {
top.value = elementY.value - 100
}
// 控制背景圖移動(dòng)
// 1. 蒙層移動(dòng)的方向和大圖背景移動(dòng)的方向是相反的
// 2. 蒙層和大圖由于面積大小是1:2的 蒙層每移動(dòng)一個(gè)像素 大圖移動(dòng)倆個(gè)像素
// backgrondPosition:x,y;
position.backgroundPositionX = -left.value * 2 + 'px'
position.backgroundPositionY = -top.value * 2 + 'px'
// 當(dāng)isOutside的值發(fā)生變化的時(shí)候,立刻取反賦值給isShow
// isOutside: 是否在容器外部 外部true 內(nèi)部 false
isShow.value = !isOutside.value
},
{}
)
return {
curId,
target,
left,
top,
position,
isShow
}
}
}
</script>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在Vue?3中使用OpenLayers加載GPX數(shù)據(jù)并顯示圖形效果
本文介紹了如何在Vue 3中使用OpenLayers加載GPX格式的數(shù)據(jù)并在地圖上顯示圖形,通過使用OpenLayers的GPX解析器,我們能夠輕松地處理和展示來自GPS設(shè)備的地理數(shù)據(jù),需要的朋友可以參考下2024-12-12
手把手搭建安裝基于windows的Vue.js運(yùn)行環(huán)境
手把手教大家搭建安裝基于windows的Vue.js的運(yùn)行環(huán)境,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
vue.js 圖片上傳并預(yù)覽及圖片更換功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue.js 圖片上傳并預(yù)覽及圖片更換功能,小編主要圍繞我們?nèi)粘J褂霉δ艿睦幼鲋v解,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08
Vue?2?和?Vue?3?中?toRefs函數(shù)的不用用法
Vue?是一款流行的JavaScript?框架,用于構(gòu)建用戶界面,在Vue2和?Vue3中,都存在一個(gè)名為toRefs的函數(shù),但其行為在這兩個(gè)版本中有所不同,這篇文章主要介紹了Vue2和Vue3中toRefs的區(qū)別,需要的朋友可以參考下2023-08-08
Nuxt.js結(jié)合Serverless構(gòu)建無服務(wù)器應(yīng)用
Nuxt.js是一個(gè)基于Vue.js的框架,結(jié)合Serverless架構(gòu),Nuxt.js可以讓你構(gòu)建高度可擴(kuò)展、成本效益高的無服務(wù)器應(yīng)用,具有一定的參考價(jià)值,感興趣的可以了解一下2024-08-08
vue 導(dǎo)航錨點(diǎn)_點(diǎn)擊平滑滾動(dòng),導(dǎo)航欄對(duì)應(yīng)變化詳解
這篇文章主要介紹了vue 導(dǎo)航錨點(diǎn)_點(diǎn)擊平滑滾動(dòng),導(dǎo)航欄對(duì)應(yīng)變化詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
element-resize-detector監(jiān)聽普通元素的實(shí)現(xiàn)示例
當(dāng)涉及到網(wǎng)頁元素的實(shí)時(shí)尺寸變化監(jiān)測(cè)時(shí),element-resize-detector?是一個(gè)值得推薦的開源庫,本文主要介紹了element-resize-detector監(jiān)聽普通元素的實(shí)現(xiàn)示例,感興趣的可以了解一下2024-07-07

