Detailed explanation of the dialog element in HTML5 (code example)

不言
Release: 2018-10-13 14:51:08
forward
3531 people have browsed it

This article brings you a detailed explanation (code example) of the dialog element in HTML5. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Dialog box (also known as modal box, floating layer) is an important part for user interaction in web projects. Our most common ones arealert(), confirm()in js, However, this dialog box is not beautiful, and the style cannot be customized, so during the development process, you usually build a wheel according to your own needs or use a third party.

Composition of dialog boxes

Common pop-up box forms:

Position: upper left corner, upper right corner, lower left corner, lower right corner of the screen, vertical centering, etc.

Size: fixed width and fixed height, fixed width and variable height, variable width and variable height, etc.

The dialog form in development is a random combination of position and size.

But there is one situation (vertical centering with variable width and height) that is not easy to implement (you can use display:table or CSS3's translate or flex to achieve it). For details, please refer to the horizontal and vertical centering layout

above The dialog box contains the content container, and there is also a mask layer (mask) under the dialog box. The mask layer is a separation layer between the dialog box and the page body formed after the user triggers the pop-up box. Its existence can give A more obvious visual difference effect for users, and it also prevents users from other unnecessary page main operations after triggering the dialog box, thereby producing a better user experience.

Problems

Although there are many dialog wheels for us to choose from, we face various problems.

  • Which dialog box to choose (a headache for people with choice syndrome)

  • Usability (api Simple or not, does it depend on other third-party libraries)

  • Scalability

  • Browser compatibility support

So, is there a simple way to make a dialog box? Of course, we can use HTML5’sdialogelement.

HTML5(dialog)

 

Hello world.

Copy after login

It’s very simple. We can use the above code to make a dialog box with the content of ‘Hello world.’ pop-up.

It is also easy to control the display/hiding of the dialog box. Add theopenattribute to display it, and remove it to hide it. Of course, we can also use the DOM interface to control the visibility (show(), close()) ofdialog

The mask layer under the dialog box, we can use: :backgroppseudo element, and to activate it, we need to use theshowModal()API of this DOM. The characteristic of::backgropis that its position is under the dialog. On top of anyz-index.

UsingshowModal()can not only display the mask layer, but also the dialog container, so when using::backdrop, you can useshowModal ()replaces theshow()API; if you press theESCkey on the keyboard, the popup layer will be closed. Of course, you can use theclose()DOM API.

We can set::backdropThis layer is a semi-transparent layer, the code is as follows:

dialog::backdrop { background-color: rgba(0, 0, 0, 0.75); }
Copy after login

In addition to our common pop-up layer of prompt information, there is also a Class comparison uses a popup layer with a form.

Pop-up layer with form

Can we use the HTML5 dialog element combined with the form element to make these pop-up layers?

Answer: Yes

How can we closely combine the form element and the dialog element?

Answer: We only need to add the attributemethod="dialog"to the dialog element, so that the value of the attributevalueof the button element can be passed Give the dialog element.

 

确定 or 取消

Copy after login

demo

var dialog = document.querySelector('dialog') dialog.showModal() dialog.addEventListener('close', function(event) { alert(dialog.returnValue) })
Copy after login
 

确定 or 取消

Copy after login
dialog::backdrop { background: rgba(0, 0, 0, 0.6) }
Copy after login

Browser compatibility

Although this is a relatively easy-to-use HTML5, there are still compatibility issues. Chrome and Opera support it better. , it is an experimental feature in Firefox, but IE, Edge, and Safari do not support it well. iOS does not support it. Android does not support it well enough. It is only supported by Android 6 and later. For details, please refer to caniuse

So, can old versions of browsers support HTML5 dialog? First of all, what we think about is whether there is a polyfill that supports dialog, just like babel-polyfill that supports the new features of es6. There is indeed such an open source project a11y-dialog, which provides different versions of vue and react respectively.

The above is the detailed content of Detailed explanation of the dialog element in HTML5 (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!