Vue3+vueuse實現放大鏡示例詳解

前言

給大傢帶來一種潮流的方式,實現放大鏡效果,安排🤩

準備工作

下包:

yarn add @vueuse/core或
npm i @vueuse/core或

放大鏡基本的結構

<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">
    <!-- 顯示在右側的放大之後的區域 -->
    <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>

功能實現

使用@vueuse/core裡面的useMouseInElement方法更多請前往vueuse官網瞭解>

<script lang="ts" setup name="GoodsImage">import { ref } from 'vue';
import {useMouseInElement} from '@vueuse/core'
const target = ref(null)
// isOutside是否進入指定區域 進入為false 否則為true
// elementX 鼠標X位置
// elementY 鼠標Y位置
const {isOutside,elementX,elementY} = useMouseInElement(target) // useMouseInElement(指定的區域)鼠標進入的位置
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">
    <!-- 顯示在右側的放大之後的區域 -->
    <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 讓光標處再中間
  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>

看看效果吧 最後一步來啦⬇️⬇️⬇️

移動遮罩大圖跟著移動

 <!-- 顯示在右側的放大之後的區域 -->
    <div class="large"
     v-show="!isOutside"
    :style="[{backgroundImage:'url('+images+')', backgroundPosition: `-${position.x*2}px -${position.y*2}px`}]"></div>

瞧瞧完成效果👍👍👍

完整實現代碼

<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是否進入指定區域 進入為false 否則為true
// elementX 鼠標X位置
// elementY 鼠標Y位置
const {isOutside,elementX,elementY} = useMouseInElement(target) // useMouseInElement(指定的區域)鼠標進入的位置
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">
    <!-- 顯示在右側的放大之後的區域 -->
    <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>

結束語

以上就是Vue3+vueuse實現放大鏡的詳細內容,更多關於Vue3 vueuse放大鏡的資料請關註WalkonNet其它相關文章!

推薦閱讀: