Table of Contents
Composition of dialog boxes
Problems
HTML5(dialog)
Pop-up layer with form
Browser compatibility
Home Web Front-end H5 Tutorial Detailed explanation of the dialog element in HTML5 (code example)

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

Oct 13, 2018 pm 02:51 PM
css3 html5

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 are alert(), 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’s dialog element.

HTML5(dialog)

<dialog open>
    <h2> Hello world.</h2>
</dialog>
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 the open attribute to display it, and remove it to hide it. Of course, we can also use the DOM interface to control the visibility (show(), close()) of dialog

The mask layer under the dialog box, we can use: :backgrop pseudo element, and to activate it, we need to use the showModal() API of this DOM. The characteristic of ::backgrop is that its position is under the dialog. On top of any z-index.

Using showModal() can not only display the mask layer, but also the dialog container, so when using ::backdrop, you can use showModal () replaces the show() API; if you press the ESC key on the keyboard, the popup layer will be closed. Of course, you can use the close() DOM API.

We can set ::backdrop This 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 attribute method="dialog" to the dialog element, so that the value of the attribute value of the button element can be passed Give the dialog element.

<dialog>
  <form method="dialog">
    <p>确定 or 取消</p>
    <button type="submit" value="yes">确定</button>
    <button type="submit" value="no">取消</button>
  </form>
</dialog>

<script>
    var dialog = document.querySelector('dialog')
    dialog.showModal()
    dialog.addEventListener('close', function(event) {
        console.log(dialog.returnValue)
    })
</script>
Copy after login

demo

var dialog = document.querySelector(&#39;dialog&#39;)
dialog.showModal()
dialog.addEventListener(&#39;close&#39;, function(event) {
  alert(dialog.returnValue)
})
Copy after login
<dialog>
  <form method="dialog">
    <p>确定 or 取消</p>
    <button type="submit" value="yes">确定</button>
    <button type="submit" value="no">取消</button>
  </form></dialog>
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!

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles