Vue3.0 handwriting magnifying glass effect

Vue3.0 handwriting magnifying glass effect

The effect to be achieved is: fixed zoom in twice, when the mouse enters the left picture area, the mask layer is displayed, when leaving, the mask layer is hidden.

Cursor in CSS

How to achieve the mouse following effect: (child is absolutely the same as father) absolute positioning + modify top, left to control movement

In @vueuse, there is a tool method: useMouseInElement

<template>
  <div ref="target">
    <h1>Hello world</h1>
  </div>
</template>
 
<script>
import { ref } from 'vue'
import { useMouseInElement } from '@vueuse/core'
 
export default {
  setup() {
    const target = ref(null)
 
    const { x, y, isOutside } = useMouseInElement(target)
 
    return { x, y, isOutside }
  }
}
</script>

This is the usage on the VueUse official website. Finally, don't forget to return { target }. I didn't return target at the beginning, so the values ​​of x, y, and isOutside were 0, 0, and false, which were not changing values.

The relationship between the mouse position and the mask position:

 <div class="layer" :style="layerStyle"></div> //This is the mask layer setup(){ //Here is the code to implement mouse following const layerStyle = reactive({
      top: '0px',
      left: '0px'
    })
    // Monitor the changes of three values, watch the first parameter with array watch([elementX, elementY, isOutside], () => {
      // layerStyle.left = elementX.value / 2 + 'px'
      // layerStyle.top = elementY.value / 2 + 'px'
      let top = elementY.value - 100
      let left = elementX.value - 100
 
      // Assign position to mask element if (top < 0) top = 0
      if (top > 200) top = 200
      if (left < 0) left = 0
      if (left > 200) left = 200
      layerStyle.top = top + 'px'
      layerStyle.left = left + 'px'
    })
    return { elementX, elementY, isOutside, target, layerStyle }
 
}

The mask area cannot exceed the parent box on the left. There are two lines of code above that I commented out. Why can't I write them together? Because when I need to add judgment later, I will find that when I add px after the judgment, you will find that you don't know where to start. If you write them separately like above, top and left are just a value. After the calculation is completed, add the unit.

How to achieve the amplification effect:

There is a background-size attribute in the css style. The first parameter refers to the width and the second parameter refers to the height. You can enlarge the image

The original size is 400*400, so if it is doubled, it will be 800*800

The background-position-x and background-position-y in the CSS style can enlarge the specified area

About background-position: x,y The first value is the horizontal position and the second value is the vertical position

Here is the div on the right with the zoom effect:

 <div class="large" :style="{ backgroundImage:`url(${images[current]})`,...largeStyle }"></div> 

This is the css code, you can refer to it:

 .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;
 }

Finally: When the mouse moves out of the left box area, the mask is hidden and the enlarged box on the right is also hidden

The isOutSide property of useMouseInElement can detect whether it exceeds the monitoring element, just use v-show="!isOutSide"

This is the end of this article about the Vue3.0 handwriting magnifying glass effect. For more relevant Vue3.0 handwriting magnifying glass effect content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue realizes the product magnifying glass effect
  • 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
  • Vue implements magnifying glass effect
  • A handwritten vue magnifying glass effect
  • Vue3 encapsulates the magnifying glass effect component of Jingdong product details page

<<:  Implementation of sharing data between Docker Volume containers

>>:  Detailed tutorial on installing JDK8 on Linux system (CentOS7 installation)

Recommend

JS realizes video barrage effect

Use ES6 modular development and observer mode to ...

A brief discussion on two methods to solve space-evenly compatibility issues

Since its launch in 2009, flex has been supported...

Implementation of Nginx configuration https

Table of contents 1: Prepare https certificate 2:...

How to install Nginx in Docker

Install Nginx on Docker Nginx is a high-performan...

Solution to Linux server graphics card crash

When the resolution of the login interface is par...

CSS sample code with search navigation bar

This article shows you how to use CSS to create a...

The use and difference between JavaScript pseudo-array and array

Pseudo-arrays and arrays In JavaScript, except fo...

MySQL should never write update statements like this

Table of contents Preface cause Phenomenon why? A...

Python Flask WeChat applet login process and login api implementation code

1. Let’s take a look at the effect first Data ret...

How to configure wordpress with nginx

Before, I had built WordPress myself, but at that...

Some tips on deep optimization to improve website access speed

<br />The website access speed can directly ...

Discussion on more reasonable creation rules for MySQL string indexes

Preface Regarding the use of MySQL indexes, we ha...

Clean XHTML syntax

Writing XHTML demands a clean HTML syntax. Writing...

IDEA uses the Docker plug-in (novice tutorial)

Table of contents illustrate 1. Enable Docker rem...