Vue3 realizes the image magnifying glass effect

Vue3 realizes the image magnifying glass effect

This article example shares the specific code of Vue3 to achieve the image magnifying glass effect for your reference. The specific content is as follows

Achieve results

Code

<template>
  <div class="goods-image">
    <!-- Large image container -->
    <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="" />
      <!-- Mask container -->
      <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: the offset value of the mouse from the left side // elementY: the offset value of the table from the top // isOutside: whether it is outside the container (true for outside and false for inside)
    const { elementX, elementY, isOutside } = useMouseInElement(target)
    const left = ref(0) // The distance from the slider to the left const top = ref(0) // The distance from the slider to the top const isShow = ref(false) // Show and hide the large image and the mask image const position = reactive({ // The position of the large image display, the default is 0
      backgroundPositionX: 0,
      backgroundPositionY: 0
    })
    watch(
      //Monitoring object [elementX, elementY, isOutside],
      () => {
        if (elementX.value < 100) {
          // Minimum distance on the left 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) {
          // Minimum distance on the left top.value = 0
        }
        if (elementY.value > 300) {
          top.value = 200
        }
        if (elementY.value > 100 && elementY.value < 300) {
          top.value = elementY.value - 100
        }
        // Control the movement of the background image // 1. The direction of the mask layer's movement is opposite to that of the background image // 2. Since the area ratio of the mask layer and the background image is 1:2, the background image moves two pixels for every pixel the mask layer moves // backgrondPosition: x, y;
        position.backgroundPositionX = -left.value * 2 + 'px'
        position.backgroundPositionY = -top.value * 2 + 'px'
 
        // When the value of isOutside changes, immediately negate it and assign it to isShow
        // isOutside: Is it outside the container? True for outside and false for inside
        isShow.value = !isOutside.value
      },
      {}
    )
    return {
      curId,
      target,
      left,
      top,
      position,
      isShow
    }
  }
}
</script>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Vue realizes the product magnifying glass effect
  • Example code of Vue3 encapsulated magnifying glass component
  • Vue implements the magnifying glass function of the product details page
  • Vue implements the magnifying glass effect of tab switching
  • Vue implements a simple magnifying glass effect
  • Vue implements a search box with a magnifying glass
  • Vue3.0 handwriting magnifying glass effect
  • Vue implements magnifying glass effect
  • A handwritten vue magnifying glass effect
  • Vue3 encapsulates the magnifying glass effect component of Jingdong product details page

<<:  Detailed explanation of common operations of Docker images and containers

>>:  Installation of various versions of MySQL 8.0.18 and problems encountered during installation (essence summary)

Blog    

Recommend

Vue two fields joint verification to achieve the password modification function

Table of contents 1. Introduction 2. Solution Imp...

Write your HTML like this to make your code more compatible

For example, users who need screen reading softwar...

18 sets of exquisite Apple-style free icon materials to share

Apple Mug Icons and Extras HD StorageBox – add on...

Sharing experience on MySQL slave maintenance

Preface: MySQL master-slave architecture should b...

Syntax alias problem based on delete in mysql

Table of contents MySQL delete syntax alias probl...

Differences and comparisons of storage engines in MySQL

MyISAM storage engine MyISAM is based on the ISAM...

Basic usage examples of listeners in Vue

Table of contents Preface 1. Basic usage of liste...

Tutorial on deploying jdk and tomcat on centos7 without interface

1. Install xshell6 2. Create a server connection ...

MYSQL A question about using character functions to filter data

Problem description: structure: test has two fiel...

VMware virtual machine installation Linux system graphic tutorial

This article shares the specific steps of VMware ...

Some experience in building the React Native project framework

React Native is a cross-platform mobile applicati...

Tutorial on installing Ubuntu 20.04 and NVIDIA drivers

Install Ubuntu 20.04 Install NVIDIA drivers Confi...

Fixed a bug caused by scrollbar occupying space

background This bug was caused by滾動條占據空間. I check...

How to enter directory/folder in Linux without using CD command

As we all know, without the cd command, we cannot...

A simple LED digital clock implementation method in CSS3

This should be something that many people have do...