Home Web Front-end HTML Tutorial About the weird behavior of pointer-events attribute in css_html/css_WEB-ITnose

About the weird behavior of pointer-events attribute in css_html/css_WEB-ITnose

Jun 24, 2016 am 11:50 AM

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>
Copy after login

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:;

  • auto-------Default value, the mouse will not penetrate the current layer
  • For SVG: visiblePainted*, visibleFill*, visibleStroke*, visible*,painted*, fill*, stroke*, all*
  • 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!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

    The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

    Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

    HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

    What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

    The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

    What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

    The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

    What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

    The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

    What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

    The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

    The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

    HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

    What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

    AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

    See all articles