Vue realizes the product magnifying glass effect

Vue realizes the product magnifying glass effect

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

1. Introduction

In this prosperous e-commerce era, with all kinds of live streaming or self-service shopping, we have a better understanding of the products and further checked the details. We found that our products can be magnified. So, we used the front-end technology Vue framework and wrote a function similar to a magnifying glass.

2. Implementation ideas

For the display space of the original image (left), you can replace the img with canvas to protect the image row.
The enlarged indicator area is displayed as the mouse moves (mouse layer mask top), and the enlarged display space is selected in the display layer mask area (right)

3. Effect display

4. Specific implementation logic code

template (remember to change the image path)

<template>
  <div>
    <div class="left">
      <img class="leftImg" src="../../src/assets/curry.jpg" alt="" />
      <!-- Mouse layer cover-->
      <div v-show="topShow" class="top" :style="topStyle"></div>
      <!-- The top layer covers the entire original image space with a transparent layer -->
      <div
        class="maskTop"
        @mouseenter="enterHandler"
        @mousemove="moveHandler"
        @mouseout="outHandler"
      ></div>
    </div>
    <div v-show="rShow" class="right">
      <img
        :style="r_img"
        class="rightImg"
        src="../../src/assets/curry.jpg"
        alt=""
      />
    </div>
  </div>
</template>

style css

<style scoped>
/* Enlarge the image and position the upper left corner to (0,0) */
.rightImg {
  display: inline-block;
  width: 800px;
  height: 800px;
  position: absolute;
  top: 0;
  left: 0;
} /* Image zoom space on the right*/
.right {
  margin-left: 412px;
  width: 400px;
  height: 400px;
  border: 1px solid red;
  position: relative;
  overflow: hidden;
} /* A top-level mask */
.maskTop {
  width: 400px;
  height: 400px;
  position: absolute;
  z-index: 1;
  top: 0;
  left: 0;
} /* Layer mask, position the upper left corner to (0,0) */
.top {
  width: 200px;
  height: 200px;
  background-color: lightcoral;
  opacity: 0.4;
  position: absolute;
  top: 0;
  left: 0;
} /* Display of original image */
.leftImg {
  width: 400px;
  height: 400px;
  display: inline-block;
} /* Container of the original image */
.left {
  width: 400px;
  height: 400px;
  border: 1px solid teal;
  float: left;
  position: relative;
}
</style>

script core js

<script>
export default {
  data() {
    return {
      topStyle: { transform: "" },
      r_img: {},
      topShow: false,
      rShow: false,
    };
  },
  methods: {
    // The mouse enters the original image space function enterHandler() {
      // Display of layer mask and magnified space this.topShow = true;
      this.rShow = true;
    },
    // Mouse movement function moveHandler(event) {
      // Mouse coordinate position let x = event.offsetX;
      let y = event.offsetY;
      // The coordinate position of the upper left corner of the layer mask, and limit it: it cannot exceed the upper left corner of the original image area let topX = x - 100 < 0 ? 0 : x - 100;
      let topY = y - 100 < 0 ? 0 : y - 100;
      // Restrict the position of the layer mask again to ensure that the layer mask can only be within the original image area if (topX > 200) {
        topX = 200;
      }
      if (topY > 200) {
        topY = 200;
      }
      // Move control through transform this.topStyle.transform = `translate(${topX}px,${topY}px)`;
      this.r_img.transform = `translate(-${2 * topX}px,-${2 * topY}px)`;
    },
    // Mouse out function outHandler() {
      //Control the hiding of layer cover and magnification space this.topShow = false;
      this.rShow = false;
    },
  },
};
</script>

V. Summary and Thoughts

I originally added three mouse events to the left of the original image container, but problems kept occurring.

1. I added a transparent maskTop covering the mouse area to make the magnifying glass fully realized. If I don’t add this maskTop layer, when my mouse enters the original image area, the mouse mask will not move with the mouse, and will vibrate at a high frequency when the mouse moves. The magnified area on the right will not move smoothly.

2. If the maskTop layer is not added, when I move the mouse into the original image area, the mousemove event is only executed once, which seems to be because the mouse layer mask blocks the

3. I have tried to dynamically determine the initial position of the mouse mask before and put it in the mouseenter event. As a result, the mouseenter event was executed abnormally many times, as if it had become a mousemove event.

I have seen other magnifying glass cases, but none of them need to add the top-level overlay mask. I hope someone passing by can help me solve this problem.

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:
  • Example code of Vue3 encapsulated magnifying glass component
  • Vue3 realizes the image magnifying glass effect
  • 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 the principle and implementation process of Nginx configuration https

>>:  How to assign default values ​​to fields when querying MySQL

Recommend

MySQL query optimization using custom variables

Table of contents Optimizing sorting queries Avoi...

Tutorial diagram of installing mysql8.0.18 under linux (Centos7)

1 Get the installation resource package mysql-8.0...

How to modify the initial password of MySQL on MAC

Problem description: I bought a Mac and installed...

Analysis of the configuration process of installing mariadb based on docker

1. Installation Search the mariadb version to be ...

A preliminary understanding of CSS custom properties

Today, CSS preprocessors are the standard for web...

mysql5.7.18.zip Installation-free version configuration tutorial (windows)

This is the installation tutorial of mysql5.7.18....

Mysql sorting and paging (order by & limit) and existing pitfalls

Sorting query (order by) In e-commerce: We want t...

Solve the problem of Syn Flooding in MySQL database

Syn attack is the most common and most easily exp...

How to solve the abnormal error ERROR: 2002 in mysql

Recently, an error occurred while starting MySQL....

VScode Remote SSH remote editing and debugging code

The latest Insider version of Visual Studio Code ...

MySQL 8.0.11 Installation Tutorial under Windows

This article records the installation tutorial of...

jQuery achieves fade-in and fade-out effects

Before using jQuery to complete the fade-in and f...