如何利用vue3實現放大鏡效果,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
逛購物網站的時候,想必大家都見過鼠標放到商品上,會有一個放大的效果。今天我們就自己動手封裝一個放大鏡效果的全局組件,一起來看下吧~
從技術角度
通過vue插件方式封裝為全局組件,整個項目其他位置也可以使用,且使用方便
模塊化開發思想,一個模塊實現一個功能
用戶角度
可以帶來更好的瀏覽體驗
可以看到商品的細節
需要用到@vueuse/core的useMouseInElement方法,所以先在項目根目錄下打開終端執行如下命令
這里安裝的指定版本的,各位小伙伴兒按需選擇
npm install @vueuse/core@5.3.0
還是像之前的文章一樣,使用vue插件的方式注冊全局組件
在src/components下存放封裝的全局組件,這個目錄下新建enlarge-images.vue文件。
代碼如下(示例):
<template>
<div class="goods-image">
<!-- 預覽大圖 -->
<div class="large" :style='[{backgroundImage: `url(${images[currIndex]})`}, bgPosition]' v-show='isShow'></div>
<div class="middle" ref='target'>
<!-- 左側的大圖 -->
<img :src="images[currIndex]" alt="">
<!-- 遮罩層 -->
<div class="layer" :style='[position]' v-show='isShow'></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: 'EnlargeImages',
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
}
// console.log(elementX.value, elementY.value, isOutside.value)
// 計算預覽大圖背景的位置
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 {
box-sizing: border-box;
width: 480px;
height: 400px;
position: relative;
display: flex;
z-index: 500;
img {
width: 100%;
height: 100%;
}
.large {
position: absolute;
top: 0;
left: 410px;
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, 0.2);
left: 0;
top: 0;
position: absolute;
}
}
.small {
margin: 0;
padding: 0;
width: 80px;
li {
width: 68px;
height: 68px;
margin: 10px;
list-style: none;
cursor: pointer;
&:hover,
&.active {
border: 2px solid #27ba9b;
}
}
}
}
</style>src/components下新建index.js
import EnlargeImages from './enlarge-images.vue'
export default {
install (app) {
app.component(EnlargeImages.name, EnlargeImages)
}
}main.js中注冊為插件
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
// 自己封裝的
import myUI from './components'
createApp(App).use(store).use(router).use(myUI).mount('#app')這里借助固定的數據進行測試
代碼如下(示例):
<template>
<div class="home-banner">
<!-- 放大鏡效果 -->
<enlarge-images :images="images"/>
</div>
</template>
<script>
export default {
name: 'App',
setup() {
const images = [
'https://code-1307161657.cos.ap-beijing.myqcloud.com/images%2Fcloud.jpeg',
'https://code-1307161657.cos.ap-beijing.myqcloud.com/images%2Fground.jpeg',
'https://code-1307161657.cos.ap-beijing.myqcloud.com/images%2Fnight.jpeg',
'https://code-1307161657.cos.ap-beijing.myqcloud.com/images%2Fstreet.jpeg',
'https://code-1307161657.cos.ap-beijing.myqcloud.com/images%2Fsun.jpeg'
]
return { images }
}
}
</script>
<style lang="less">
.home-banner {
width: 1000px;
margin: 50px auto;
}
</style>鼠標移入右側小圖片,即可切換當前顯示的圖片

鼠標放入左側圖片預覽區,預覽區內移動鼠標即可在右側看到放大的指定區域
(PS:gif圖太大了,各位看下效果圖吧~)

批量注冊為全局組件的方式,各位可以看下vue常用工具函數這篇文章。
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。