Vue3+vueuse實(shí)現(xiàn)放大鏡示例詳解
前言
給大家?guī)?lái)一種潮流的方式,實(shí)現(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="" /> <!-- 移動(dòng)遮罩 --> <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; // 可以移動(dòng) 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>
功能實(shí)現(xiàn)
使用@vueuse/core里面的useMouseInElement方法更多請(qǐng)前往vueuse官網(wǎng)了解>
<script lang="ts" setup name="GoodsImage">import { ref } from 'vue'; import {useMouseInElement} from '@vueuse/core' const target = ref(null) // isOutside是否進(jìn)入指定區(qū)域 進(jìn)入為false 否則為true // elementX 鼠標(biāo)X位置 // elementY 鼠標(biāo)Y位置 const {isOutside,elementX,elementY} = useMouseInElement(target) // useMouseInElement(指定的區(qū)域)鼠標(biāo)進(jìn)入的位置 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="" /> <!-- 移動(dòng)遮罩 --> <div class="layer" ref="target" v-show="!isOutside "></div> </div> </div> </template>
看看效果使用useMouseInElement方法的效果是不是很奈斯?????? 精彩還在后面??????
`` 移動(dòng)遮罩
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 } }) <!-- 移動(dòng)遮罩 --> <div class="layer" ref="target" v-show="!isOutside " :style="{ left:position.x+'px', top: position.y+'px' }"></div>
看看效果吧 最后一步來(lái)啦??????
移動(dòng)遮罩大圖跟著移動(dòng)
<!-- 顯示在右側(cè)的放大之后的區(qū)域 --> <div class="large" v-show="!isOutside" :style="[{backgroundImage:'url('+images+')', backgroundPosition: `-${position.x*2}px -${position.y*2}px`}]"></div>
瞧瞧完成效果??????
完整實(shí)現(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是否進(jìn)入指定區(qū)域 進(jìn)入為false 否則為true // elementX 鼠標(biāo)X位置 // elementY 鼠標(biāo)Y位置 const {isOutside,elementX,elementY} = useMouseInElement(target) // useMouseInElement(指定的區(qū)域)鼠標(biāo)進(jìn)入的位置 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="" /> <!-- 移動(dòng)遮罩 --> <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; // 可以移動(dòng) 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é)束語(yǔ)
以上就是Vue3+vueuse實(shí)現(xiàn)放大鏡的詳細(xì)內(nèi)容,更多關(guān)于Vue3 vueuse放大鏡的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue 詳情跳轉(zhuǎn)至列表頁(yè)實(shí)現(xiàn)列表頁(yè)緩存
這篇文章主要介紹了vue 詳情跳轉(zhuǎn)至列表頁(yè)實(shí)現(xiàn)列表頁(yè)緩存,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03vue開發(fā)chrome插件,實(shí)現(xiàn)獲取界面數(shù)據(jù)和保存到數(shù)據(jù)庫(kù)功能
這篇文章主要介紹了vue開發(fā)chrome插件,實(shí)現(xiàn)獲取界面數(shù)據(jù)和保存到數(shù)據(jù)庫(kù)功能的示例,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下2020-12-12vue如何使用window.open打開頁(yè)面并拼接參數(shù)
這篇文章主要介紹了vue如何使用window.open打開頁(yè)面并拼接參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09vue訪問(wèn)未定義的路由時(shí)重定向404問(wèn)題
這篇文章主要介紹了vue訪問(wèn)未定義的路由時(shí)重定向404問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Vue自定義過(guò)濾器格式化數(shù)字三位加一逗號(hào)實(shí)現(xiàn)代碼
這篇文章主要介紹了Vue自定義過(guò)濾器格式化數(shù)字三位加一逗號(hào)的實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-03-03Vue中component標(biāo)簽解決項(xiàng)目組件化操作
這篇文章主要介紹了Vue中component標(biāo)簽解決項(xiàng)目組件化操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09