Home>Article>Web Front-end> How to hide the current element in vuejs

How to hide the current element in vuejs

藏色散人
藏色散人 Original
2021-09-24 16:36:47 3449browse

The implementation method of hiding the current element in vuejs: 1. After the page is mounted, listen to the global click event; 2. Get the currently clicked element and obtain the attributes of the current element itself according to the needs; 3. Determine the current click Whether the element is the same as the element to be hidden; 4. If the currently clicked element is not the same as the element to be hidden, it will be hidden.

How to hide the current element in vuejs

The operating environment of this article: Windows 7 system, vue version 2.9.6, DELL G3 computer.

How to hide the current element in vuejs?

Vue implements clicking outside the current element to hide the current element (implementation idea)

1. Binding elements

2. mounted life cycle

3. Implementation idea

  • After the page is mounted, listen for global click events
  • Get the currently clicked element and obtain the attributes of the current element itself according to the requirements
  • Determine whether the currently clicked element and the element to be hidden are the same
  • The currently clicked element and the element to be hidden are the same If not the same, hide

4. Final effect

Then let’s take a look at other aspects of the page outside the target element that vue implements. Hide the pop-up window in place

Method:

Step 1: Add a click event to the outermost element p on the page: @click="popShow = false”.

Step 2: Add a click event to the click target element: @click="popShow = true".

Note: popShow is a flag that controls the display and hiding of pop-up windows.

PS: Let’s take a look at vue’s implementation of clicking elsewhere to hide p

Method 1:

By listening to events

document.addEventListener('click',function(e){ if(e.target.className!='usermessage'){ that.userClick=false; } })

Method 2 (better):

Add a click event to the outermost p@click="userClick=false "

Addto the clicked element: @click.stop="userClick=!userClick"

Method 3: