vue3封裝放大鏡組件的實(shí)例代碼
組件基礎(chǔ)結(jié)構(gòu)
結(jié)尾有完整代碼可直接復(fù)制使用
目的:封裝圖片預(yù)覽組件,實(shí)現(xiàn)鼠標(biāo)懸停切換效果

落地代碼:
<template>
<div class="goods-image">
<div class="middle">
<img :src="images[currIndex]" alt="">
</div>
<ul class="small">
<li v-for="(img,i) in images" :key="img" :class="{active:i===currIndex}">
<img @mouseenter="currIndex=i" :src="img" alt="">
</li>
</ul>
</div>
</template>
<script>
import { ref } from 'vue'
export default {
name: 'GoodsImage',
props: {
images: {
type: Array,
default: () => []
}
},
setup (props) {
const currIndex = ref(0)
return { currIndex }
}
}
</script>
<style scoped lang="less">
.goods-image {
width: 480px;
height: 400px;
position: relative;
display: flex;
.middle {
width: 400px;
height: 400px;
background: #f5f5f5;
}
.small {
width: 80px;
li {
width: 68px;
height: 68px;
margin-left: 12px;
margin-bottom: 15px;
cursor: pointer;
&:hover,&.active {
border: 2px solid @xtxColor;
}
}
}
}
</style>
圖片放大鏡
目的:實(shí)現(xiàn)圖片放大鏡功能

步驟:
- 首先準(zhǔn)備大圖容器和遮罩容器
- 然后使用@vueuse/core的useMouseInElement方法獲取基于元素的偏移量
- 計(jì)算出 遮罩容器定位與大容器背景定位 暴露出數(shù)據(jù)給模板使用
落地代碼:
<template>
<div class="goods-image">
+ // 實(shí)現(xiàn)右側(cè)大圖布局效果(背景圖放大4倍)
+ <div class="large" :style="[{backgroundImage:`url(${images[currIndex]})`}]"></div>
<div class="middle">
<img :src="images[currIndex]" alt="">
+ // 準(zhǔn)備待移動(dòng)的遮罩容器
+ <div class="layer"></div>
</div>
<ul class="small">
<li v-for="(img,i) in images" :key="img" :class="{active:i===currIndex}">
<img @mouseenter="currIndex=i" :src="img" alt="">
</li>
</ul>
</div>
</template>
<script>
import { ref } from 'vue'
export default {
name: 'GoodsImage',
props: {
images: {
type: Array,
default: () => []
}
},
setup (props) {
const currIndex = ref(0)
return { currIndex }
}
}
</script>
<style scoped lang="less">
.goods-image {
width: 480px;
height: 400px;
position: relative;
display: flex;
+ z-index: 500;
+ // 右側(cè)大圖樣式
+ .large {
+ position: absolute;
+ top: 0;
+ left: 412px;
+ width: 400px;
+ height: 400px;
+ box-shadow: 0 0 10px rgba(0,0,0,0.1);
+ background-repeat: no-repeat;
+ background-size: 800px 800px;
+ background-color: #f8f8f8;
+ }
.middle {
width: 400px;
height: 400px;
background: #f5f5f5;
+ position: relative;
+ cursor: move;
+ // 遮罩樣式
+ .layer {
+ width: 200px;
+ height: 200px;
+ background: rgba(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 @xtxColor;
}
}
}
}
</style>
安裝vueuse
npm i @vueuse/core@5.3.0
目前5.3.0版本相對(duì)穩(wěn)定
vueuse提供的監(jiān)聽(tīng)進(jìn)入指定范圍方法的基本使用
import { useMouseInElement } from '@vueuse/core'
const { elementX, elementY, isOutside } = useMouseInElement(target)
方法的參數(shù)target表示被監(jiān)聽(tīng)的DOM對(duì)象;返回值elementX, elementY表示被監(jiān)聽(tīng)的DOM的左上角的位置信息left和top;isOutside表示是否在DOM的范圍內(nèi),true表示在范圍之外。false表示范圍內(nèi)。
功能實(shí)現(xiàn)
<div v-if="isShow" class="large" :style="[{ backgroundImage: `url(${images[currIndex]})` }, bgPosition]"></div>
<div class="middle" ref="target">
<img :src="images[currIndex]" alt="" />
<div class="layer" v-if="isShow" :style="[position]"></div>
</div>
setup () {
// 被監(jiān)聽(tīng)的區(qū)域
const target = ref(null)
// 控制遮罩層和預(yù)覽圖的顯示和隱藏
const isShow = ref(false)
// 定義遮罩的坐標(biāo)
const position = reactive({
left: 0,
top: 0
})
// 右側(cè)預(yù)覽大圖的坐標(biāo)
const bgPosition = reactive({
backgroundPositionX: 0,
backgroundPositionY: 0
})
return { position, bgPosition, target, isShow }
}
const { elementX, elementY, isOutside } = useMouseInElement(target)
// 基于偵聽(tīng)器偵聽(tīng)值的變化
watch([elementX, elementY, isOutside], () => {
// 通過(guò)標(biāo)志位控制顯示和隱藏
isShow.value = !isOutside.value
if (isOutside.value) return
// X方向坐標(biāo)范圍控制
if (elementX.value < 100) {
// 左側(cè)
position.left = 0
} else if (elementX.value > 300) {
// 右側(cè)
position.left = 200
} else {
// 中間
position.left = elementX.value - 100
}
// Y方向坐標(biāo)范圍控制
if (elementY.value < 100) {
position.top = 0
} else if (elementY.value > 300) {
position.top = 200
} else {
position.top = elementY.value - 100
}
// 計(jì)算預(yù)覽大圖的移動(dòng)的距離
bgPosition.backgroundPositionX = -position.left * 2 + 'px'
bgPosition.backgroundPositionY = -position.top * 2 + 'px'
// 計(jì)算遮罩層的位置
position.left = position.left + 'px'
position.top = position.top + 'px'
})
完整代碼
<template>
<div class="goods-image">
<div v-if="isShow" class="large" :style="[{ backgroundImage: `url(${images[currIndex]})` }, bgPosition]"></div>
<div class="middle" ref="target">
<img :src="images[currIndex]" alt="" />
<div class="layer" v-if="isShow" :style="[position]"></div>
</div>
<ul class="small">
<li v-for="(img, i) in images" :key="img" :class="{ active: i === currIndex }">
<img @mouseenter="currIndex = i" :src="img" alt="" />
</li>
</ul>
</div>
</template>
<script>
import { ref, watch, reactive } from 'vue'
import { useMouseInElement } from '@vueuse/core'
export default {
name: 'GoodsImage',
props: {
images: {
type: Array,
default: () => []
}
},
setup (props) {
const currIndex = ref(0)
const target = ref(null)
const isShow = ref(false)
const position = reactive({
left: 0,
top: 0
})
const bgPosition = reactive({
backgroundPositionX: 0,
backgroundPositionY: 0
})
const { elementX, elementY, isOutside } = useMouseInElement(target)
watch([elementX, elementY, isOutside], () => {
isShow.value = !isOutside.value
if (isOutside.value) return
if (elementX.value <= 100) {
position.left = 0
} else if (elementX.value >= 300) {
position.left = 200
} else {
position.left = elementX.value - 100
}
if (elementY.value <= 100) {
position.top = 0
} else if (elementY.value >= 300) {
position.top = 200
} else {
position.top = elementY.value - 100
}
bgPosition.backgroundPositionX = -position.left * 2 + 'px'
bgPosition.backgroundPositionY = -position.top * 2 + 'px'
position.left += 'px'
position.top += 'px'
})
return { currIndex, target, isShow, position, bgPosition }
}
}
</script>
<style scoped lang="less">
.goods-image {
width: 480px;
height: 400px;
position: relative;
display: flex;
z-index: 500;
.large {
position: absolute;
top: 0;
left: 412px;
width: 400px;
height: 400px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
background-repeat: no-repeat;
background-size: 800px 800px;
background-color: #f8f8f8;
}
.middle {
width: 400px;
height: 400px;
background: #f5f5f5;
position: relative;
cursor: move;
.layer {
width: 200px;
height: 200px;
background: rgba(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 @xtxColor;
}
}
}
}
</style>
總結(jié)
到此這篇關(guān)于vue3封裝放大鏡組件的文章就介紹到這了,更多相關(guān)vue3封裝放大鏡組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue?element?el-select下拉滾動(dòng)加載的方法
很多朋友都遇到這樣一個(gè)問(wèn)題在使用vue+element的el-select下拉框加載返回?cái)?shù)據(jù)太多時(shí),會(huì)造成卡頓,用戶(hù)體驗(yàn)欠佳,這篇文章主要介紹了vue?element?el-select下拉滾動(dòng)加載方法,需要的朋友可以參考下2022-11-11
Vue中動(dòng)態(tài)綁定Ref的兩種方式總結(jié)
Vue3中的ref是一種創(chuàng)建響應(yīng)式引用的方式,它在Vue生態(tài)系統(tǒng)中扮演著重要角色,下面這篇文章主要給大家介紹了關(guān)于Vue中動(dòng)態(tài)綁定Ref的兩種方式,需要的朋友可以參考下2024-09-09
關(guān)于配置babel-plugin-import報(bào)錯(cuò)的坑及解決
這篇文章主要介紹了關(guān)于配置babel-plugin-import報(bào)錯(cuò)的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
使用Element實(shí)現(xiàn)表格表頭添加搜索圖標(biāo)和功能
這篇文章主要介紹了使用Element實(shí)現(xiàn)表格表頭添加搜索圖標(biāo)和功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Vue CLI4.0 webpack配置屬性之productionSourceMap用法
這篇文章主要介紹了Vue CLI4.0 webpack配置屬性之productionSourceMap用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
關(guān)于element-ui中@selection-change執(zhí)行兩次的問(wèn)題
這篇文章主要介紹了關(guān)于element-ui中@selection-change執(zhí)行兩次的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08

