Vue3實(shí)現(xiàn)圖片放大鏡效果
更新時(shí)間:2021年08月18日 14:55:44 作者:WQ佩奇大哥
這篇文章主要為大家詳細(xì)介紹了Vue3實(shí)現(xiàn)圖片放大鏡效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(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)聽(tīng)的對(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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue關(guān)于自定義指令與v-if沖突的問(wèn)題
這篇文章主要介紹了vue關(guān)于自定義指令與v-if沖突的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08vue手寫(xiě)加載動(dòng)畫(huà)項(xiàng)目
這篇文章主要為大家詳細(xì)介紹了vue手寫(xiě)加載動(dòng)畫(huà)項(xiàng)目,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10vue2.0+webpack環(huán)境的構(gòu)造過(guò)程
本文分步驟給大家介紹了vue2.0+webpack環(huán)境的構(gòu)造過(guò)程的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11