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

Detailed explanation of the concept of docker container layers

Table of contents 01 Container consistency 02 Con...

A brief analysis of JS original value and reference value issues

Primitive values ​​-> primitive types Number S...

Detailed explanation of the use of bus in Vue

Vue bus mechanism (bus) In addition to using vuex...

40 web page designs with super large fonts

Today's web designs tend to display very larg...

Ubuntu installation cuda10.1 driver implementation steps

1. Download cuda10.1: NVIDIA official website lin...

Sample code for a simple seamless scrolling carousel implemented with native Js

There are many loopholes in the simple seamless s...

Detailed explanation of how to write mysql not equal to null and equal to null

1. Table structure 2. Table data 3. The query tea...

jQuery implements the bouncing ball game

This article shares the specific code of jQuery t...

Detailed explanation of docker nginx container startup and mounting to local

First, the structure inside the nginx container: ...

JavaScript canvas to achieve code rain effect

This article shares the specific code for canvas ...

Sample code on how to implement page caching in vue mobile project

background On mobile devices, caching between pag...

Detailed explanation of the principles and usage of MySQL stored procedures

This article uses examples to explain the princip...