Vue3+vueuse實現(xiàn)放大鏡示例詳解
前言
給大家?guī)硪环N潮流的方式,實現(xiàn)放大鏡效果,安排??
準(zhǔn)備工作
下包:
yarn add @vueuse/core或 npm i @vueuse/core或
放大鏡基本的結(jié)構(gòu)
<script lang="ts" setup">
import { ref } from 'vue';
const target = ref(null)
const images = ref('https://images.mepai.me/app/works/178221/2022-07-14/w_62d01aa163e45/062d01aa163f41.jpg!1200w.jpg')
const active = ref(0)
</script>
<template>
<div class="goods-image">
<!-- 顯示在右側(cè)的放大之后的區(qū)域 -->
<div class="large"
v-show="true"
:style="[{backgroundImage:'url('+images+')'}]"></div>
<div class="middle" ref="target">
<img :src="images" alt="" />
<!-- 移動遮罩 -->
<div class="layer" ref="target" v-show="true "></div>
</div>
</div>
</template>
<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;
img{
width: 400px;
height: 400px;
}
.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 red;
}
}
}
}
</style>
功能實現(xiàn)
使用@vueuse/core里面的useMouseInElement方法更多請前往vueuse官網(wǎng)了解>

<script lang="ts" setup name="GoodsImage">import { ref } from 'vue';
import {useMouseInElement} from '@vueuse/core'
const target = ref(null)
// isOutside是否進入指定區(qū)域 進入為false 否則為true
// elementX 鼠標(biāo)X位置
// elementY 鼠標(biāo)Y位置
const {isOutside,elementX,elementY} = useMouseInElement(target) // useMouseInElement(指定的區(qū)域)鼠標(biāo)進入的位置
const images = ref('https://images.mepai.me/app/works/178221/2022-07-14/w_62d01aa163e45/062d01aa163f41.jpg!1200w.jpg')
</script>
<template>
{{isOutside}}
X: {{elementX}}
Y: {{elementY}}
<div class="goods-image">
<!-- 顯示在右側(cè)的放大之后的區(qū)域 -->
<div class="large"
v-show="!isOutside"
:style="[{backgroundImage:'url('+images+')'}]"></div>
<div class="middle" ref="target">
<img :src="images" alt="" />
<!-- 移動遮罩 -->
<div class="layer" ref="target" v-show="!isOutside "></div>
</div>
</div>
</template>
看看效果使用useMouseInElement方法的效果是不是很奈斯?????? 精彩還在后面??????

`` 移動遮罩
import {useMouseInElement} from '@vueuse/core'
import { computed } from '@vue/reactivity';
const {isOutside,elementX,elementY} = useMouseInElement(target)
const position = computed(()=>{
let x = elementX.value -100 // -100 讓光標(biāo)處再中間
let y = elementY.value -100
// 邊界處理
x = x<0 ? 0 : x
y = y<0 ? 0 : y
x = x>200 ? 200 : x
y = y>200 ? 200 : y
return {
x,
y
}
})
<!-- 移動遮罩 -->
<div class="layer" ref="target" v-show="!isOutside "
:style="{ left:position.x+'px', top: position.y+'px' }"></div>
看看效果吧 最后一步來啦??????

移動遮罩大圖跟著移動
<!-- 顯示在右側(cè)的放大之后的區(qū)域 -->
<div class="large"
v-show="!isOutside"
:style="[{backgroundImage:'url('+images+')', backgroundPosition: `-${position.x*2}px -${position.y*2}px`}]"></div>
瞧瞧完成效果??????

完整實現(xiàn)代碼
<script lang="ts" setup name="GoodsImage">import { ref } from 'vue';
import {useMouseInElement} from '@vueuse/core'
import { computed } from '@vue/reactivity';
const target = ref(null)
// isOutside是否進入指定區(qū)域 進入為false 否則為true
// elementX 鼠標(biāo)X位置
// elementY 鼠標(biāo)Y位置
const {isOutside,elementX,elementY} = useMouseInElement(target) // useMouseInElement(指定的區(qū)域)鼠標(biāo)進入的位置
const active = ref(0)
const images = ref('https://images.mepai.me/app/works/178221/2022-07-14/w_62d01aa163e45/062d01aa163f41.jpg!1200w.jpg')
const position = computed(()=>{
let x = elementX.value -100
let y = elementY.value -100
x = x<0 ? 0 : x
y = y<0 ? 0 : y
x = x>200 ? 200 : x
y = y>200 ? 200 : y
return {
x,
y
}
})
</script>
<template>
<!-- {{isOutside}}
X: {{elementX}}
Y: {{elementY}} -->
<div class="goods-image">
<!-- 顯示在右側(cè)的放大之后的區(qū)域 -->
<div class="large"
v-show="!isOutside"
:style="[{backgroundImage:'url('+images+')', backgroundPosition: `-${position.x*2}px -${position.y*2}px`}]"></div>
<div class="middle" ref="target">
<img :src="images" alt="" />
<!-- 移動遮罩 -->
<div class="layer" ref="target" v-show="!isOutside " :style="{ left:position.x+'px', top: position.y+'px' }"></div>
</div>
</div>
</template>
<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;
img{
width: 400px;
height: 400px;
}
.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 red;
}
}
}
}
</style>
結(jié)束語
以上就是Vue3+vueuse實現(xiàn)放大鏡的詳細內(nèi)容,更多關(guān)于Vue3 vueuse放大鏡的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
el-table點擊某一行高亮并顯示小圓點的實現(xiàn)代碼
這篇文章主要介紹了el-table點擊某一行高亮并顯示小圓點,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
vue 添加和編輯用同一個表單,el-form表單提交后清空表單數(shù)據(jù)操作
這篇文章主要介紹了vue 添加和編輯用同一個表單,el-form表單提交后清空表單數(shù)據(jù)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
vue使用多級彈窗(Dialog)出現(xiàn)蒙版遮罩問題及解決
這篇文章主要介紹了vue使用多級彈窗(Dialog)出現(xiàn)蒙版遮罩問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
webpack 3 + Vue2 使用dotenv配置多環(huán)境的步驟
這篇文章主要介紹了webpack 3 + Vue2 使用dotenv配置多環(huán)境,env文件在配置文件都可以用, vue頁面用的時候需要在 webpack.base.conf.js 重新配置,需要的朋友可以參考下2023-11-11
vue+elementUI實現(xiàn)多文件上傳與預(yù)覽功能實戰(zhàn)記錄(word/PDF/圖片/docx/doc/xlxs/txt)
這篇文章主要給大家介紹了關(guān)于利用vue+elementUI實現(xiàn)多文件上傳與預(yù)覽功能的相關(guān)資料,包括word/PDF/圖片/docx/doc/xlxs/txt等格式文件上傳,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-08-08
vue.js動態(tài)數(shù)據(jù)綁定學(xué)習(xí)筆記
這篇文章主要為大家詳細介紹了vue.js動態(tài)數(shù)據(jù)綁定學(xué)習(xí)筆記,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05

