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)

Recommend

Win32 MySQL 5.7.27 installation and configuration method graphic tutorial

The installation tutorial of MySQL 5.7.27 is reco...

How to create a database in navicat 8 for mysql

When developing a website, you often need to use ...

Detailed steps to install and uninstall Apache (httpd) service on centos 7

uninstall First, confirm whether it has been inst...

Docker image compression and optimization operations

The reason why Docker is so popular nowadays is m...

An example of how Tomcat manages Session

Learned ConcurrentHashMap but don’t know how to a...

Two implementation codes of Vue-router programmatic navigation

Two ways to navigate the page Declarative navigat...

User experience analysis of facebook dating website design

<br />Related article: Analysis of Facebook&...

Docker build PHP environment tutorial detailed explanation

Docker installation Use the official installation...

Docker container monitoring and log management implementation process analysis

When the scale of Docker deployment becomes large...

A brief analysis of mysql index

A database index is a data structure whose purpos...

The iframe frame sets the white background to transparent in IE browser

Recently, I need to frequently use iframe to draw ...

Detailed code examples of seven methods for vertical centering with CSS

When we edit a layout, we usually use horizontal ...

Summary of Problems in Installation and Usage of MySQL 5.7.19 Winx64 ZIP Archive

Today I learned to install MySQL, and some proble...

MySQL backup table operation based on Java

The core is mysqldump and Runtime The operation i...