摘要

在很多场景下,都须要弹窗用于交互,个别UI框架都有模态框,如果你做一个小单页,不引入UI库,你将无奈应用模态框,或者应用JavaScript自带的alert弹出揭示,或者是本人写,这都不是很便当。

dialog 是HTML5新增的语义化双标签,用于展现一个交互式的模态对话框。这是浏览器自带的模态框,十分好用。

上代码

<!DOCTYPE html><html><head>    <meta charset="utf-8">    <meta name="viewport" content="width=device-width, initial-scale=1">    <title>dialog</title>    <style>        *{            padding: 0;            margin: 0;        }        #modal {            border: none;            border-radius: 8px;            margin: 0 auto;            padding: 20px;            position: fixed;            top: -100%;            left: 20%;            transform: translateX(-50%);            animation: slideIn 0.3s forwards;        }        @keyframes slideIn {            from {                top: -100%;            }            to {                top: 10%;            }        }        #modal .modal-header {            width: 400px;            padding: 10px 0;            display: flex;            border-bottom: 1px solid #eee;        }        #modal .modal-header .modal-title {            flex: 1;            font-size: 20px;        }        #modal .modal-content {            border-bottom: 1px solid #eee;            padding: 20px 0;            font-size: 15px;        }        #modal .modal-header .modal-close {            width: 50px;            font-size: 23px;            text-align: right;            cursor: pointer;        }        #modal::backdrop {            position: fixed;            top: 0px;            right: 0px;            bottom: 0px;            left: 0px;            background: rgba(0, 0, 0, 0.2);        }    </style></head><body>    <button onclick="modal.showModal()">关上</button>    <dialog id="modal">        <div class="modal-body">            <div class="modal-header">                <div class="modal-title">题目</div>                <div class="modal-close" onclick="modal.close()">&times;</div>            </div>            <div class="modal-content">                这是内容,这是内容。            </div>        </div>    </dialog></body></html>

这个模态框默认款式是十分丑的,好在能够自定义款式,我对这个默认款式进行了丑化,能够说是丑小鸭变天鹅,我还退出了动画。

作者

TANKING