Home > Article > Web Front-end > About the weird behavior of pointer-events attribute in css_html/css_WEB-ITnose
In my memory, pointer-events is used for event penetration. In other words, if pointer-events:none is set to the parent element, the parent element will no longer listen to mouse events. Event (similar to touch, click, etc.).
When we need to do this, we usually want to "penetrate" the parent layer. When we directly click on the child element, the parent element will act as if nothing happened. This is information gathered from my previous body of knowledge. It doesn't seem wrong now, but it's incomplete.
It has side effects, or in other words, the application of the above knowledge is conditional. This is indeed the behavior when we apply this (pointer-events:none) attribute to elements that are in a parent-child relationship, but does it still behave like this when we apply it to two block elements in parallel layers?
This problem is caused by the application shown in the screenshot above. I'll give a simplified prototype later. First, let’s briefly introduce the problem of this scene demonstration. The picture above actually has four layers:
The page layer as the page container and the sub-layer composed of flowers and plants under the page. For the convenience of subsequent demonstrations, they are named pagesub layer.
The other layer is the button layer used to turn pages left and right. This layer is parallel to the page layer and is named float layer because it looks like it is floating on the page. There is a layer called floatsub below the float layer, which is used to wrap the small mushroom buttons.
I hope that when I click on the little mushroom, the event on the float layer will not be triggered, so I added pointer-events:none to the float layer; then the problem comes, now I click on the little mushroom and there is no response. . After monitoring, it was found that what was clicked was the flowers and plants layer in the page. I couldn't help but miss my sister. After a fierce pursuit, I finally found true love. Below I will announce this twists and turns process of seeking a sister:
pagesub is the sub-element of page, and floatpage is the sub-element of float. Their positional relationship is as shown in the figure above. The green page is the page layer, and the blue pagesub is the flower layer. The red float is the button layer
and the gray float page is the mushroom layer. (Color-blind friends can just read the text)
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> body { text-align: center; } .page { position: absolute; top : 0; left:0; height: 400px; background-color: green; width:500px; z-index: 2; } .float { width: 400px; height: 200px; position: absolute; top:50px; left: 50px; z-index: 3; pointer-events:none; background-color: red; } .pagesub { width: 90%; height: 60%; position: absolute; top:30px; left: 10px; z-index: 4; background-color: blue; color:white; } .floatsub { width:80%; height: 60%; position: absolute; top:33px; left:44px; background-color: gray; z-index: 4; color: white; } </style></head><body><div class="page">page <div class="pagesub"> pagesub </div></div><div class="float">float <div class="floatsub"> float page </div></div></body></html>
According to the above source code, I adjusted it in chrome and found that when I entered the float page ( When viewing elements in the gray) layer, it actually shows a blue pagesub layer. It’s totally unreasonable. There is a picture and the truth:
But when I tried to remove the attribute pointer-events, a miracle happened and everything returned to expectations. Why is this? Note that pointer-events will affect absolutely positioned layers and relationships! Of course, the prerequisite is a parallel relationship, not a parent-child relationship. Using pointer-events on elements in a parallel relationship will cause subsequent elements to be ignored.
Pay attention to what I said here "Absolutely positioned layers and relationships" means that elements that look like they use the pointer-events:none attribute go behind useless elements. As for whether this is really happening at the bottom level, I can't confirm it, but the test results show that this is the case.
In addition to none, let me mention several other attributes of pointer-events:;
Okay, that’s it for this sharing, post it Thank you for your hard work. Those who are rich will support you financially, and those who are good will support you personally. Thank you all!