Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the effect of clicking on the background of the mask layer and closing the mask layer in Html5

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to click on the background of the mask layer and close the effect of the mask layer in Html5. The article is very detailed and has a certain reference value. Interested friends must finish reading it!

Html Code:

There is only one button displayed on the page, a div with an ID of bg as a gray background mask layer, a div with an ID of popup as a pop-up window for red packets, and a div with an ID of close as a close button.

Show X

CSS code

There are no technical difficulties in the css code, the only thing to pay attention to is to absolutely locate the mask layer of the gray background. It would be fine if both top and lefe are 0.

Body {position: relative;} .btn {width: 100px; height: 40px; line-height: 40px; text-align: center; margin:20px auto 0; border: 1px solid # 333; border-radius: 10px;} .bg {width: 100%; height: 100%; position: fixed; top: 0 Left: 0; background-color: rgba (0,0,0, .6); display: none;} .popup {width: 260px; height: 320px; background: red; position: absolute; top: 50%; left: 50%; transform: translate (- 50%,-50%); border-radius: 15px } .popup .close {width: 30px; height: 30px; line-height: 30px; text-align: center; position: absolute; top:-40px; right: 0px; border: 1px solid # 999; border-radius: 50%; color: # 999;}

JS Code:

Var btn = document.getElementById ('btn'); var bg = document.getElementById (' bg'); var popup = document.getElementById ('popup'); var closeBtn = document.getElementById (' close'); / / Click the display button to display the pop-up window btn.addEventListener ('click', () = > {bg.style.display =' block';}) / / Click the shadow mask layer to close the pop-up window bg.addEventListener ('click', (e) = > {bg.style.display =' none'}); / / prevent the bubbling event, and clicking the pop-up window will not execute the click event popup.addEventListener of the parent element ('click', (e) = > {e.stopPropagation ();}) / / Click the closing symbol to close the pop-up window closeBtn.addEventListener ('click', (e) = > {e.stopPropagation (); bg.style.display =' none'}). The above is all the contents of the article "how to click on the background of the mask layer and close the effect of the mask layer in Html5". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report