教你使用HTML5原生对话框元素,轻松建立模态框组件

HTML 5.2草案加入了新的dialog元素。可是是一种实验技术。css

之前,若是咱们想要构建任何形式的模式对话框或对话框,咱们须要有一个背景,一个关闭按钮,将事件绑定在对话框中的方式安排咱们的标记,找到一种将消息传递出去的方式对话......这真的很复杂。对话框元素解决了上述全部问题。html

 

1、Bootstrap模态框和原生模态框的对比git

下面是一个bootstrap模态框的html结构:github

<!-- 按钮触发模态框 -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
    开始演示模态框
</button>
<!-- 模态框(Modal) -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    模态框(Modal)标题
                </h4>
            </div>
            <div class="modal-body">
                在这里添加一些文本
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary">
                    提交更改
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

 

下面是一个原生模态框的HTML结构:web

<!-- 按钮触发模态框 -->
<button type="button" class="btn">显示模态框</button>

<!-- 模态框 -->
<dialog open>
    HTML5原生模态框
</dialog>

 

2、基初的模态框样式bootstrap

咱们已经看到了对话框元素的最简单标记,您可能已经注意到open是上面对话框中属性。将该属性添加到元素将强制显示对话框,不然将删除它。该对话框也将绝对定位在页面上。api

上图展现了一个最基本的模态框样式。浏览器

 

打开浏览器能够查看到它的基本样式是这样的:this

dialog{
    display: block;
    position: absolute;
    left: 0px;
    right: 0px;
    width: -webkit-fit-content;
    height: -webkit-fit-content;
    color: black;
    margin: auto;
    border-width: initial;
    border-style: solid;
    border-color: initial;
    border-image: initial;
    padding: 1em;
    background: white;
}

 

dialog元素引入了一个新的伪类选择器::backdrop,经过浏览器查看到默认的::backdrop样式以下:spa

dialog::backdrop {
    position: fixed;
    top: 0px;
    right: 0px;
    bottom: 0px;
    left: 0px;
    background: rgba(0, 0, 0, 0.1);
}

 

2、设置对话框样式

咱们能够像任何HTML元素同样设置dialog元素的样式,几乎全部的CSS样式均可以。经过::backdrop伪类选择器,咱们能够用它来设置背景的样式。

例如:

dialog{
    margin-top:200px;
    width:250px;
    height:250px;
    text-align:center;
    line-height:250px;
    border-radius: 4px;
    border: none;
    box-shadow: 0 0 15px lightgray;
}
            
dialog::backdrop {
    background: rgba(black, .5);
}

 上面的样式效果以下图:

 三、对话框操做API

下面是一个基本的对话框,由于没有设置open属性,因此它不会在视觉上显示任何东西。您须要使用JavaScript API来显示/隐藏它。

<dialog>这是dialog对话框!</ dialog>

 

dialog元素的.show().close()两个api分别是显示和关闭对话框,经过DOM元素使用这两个api,您能够显示和关闭对话框。

例如:

<!-- HTML -->
<dialog>
    <p>这是dialog对话框!</p>
    <button id="close">关闭对话框</button>
</dialog>
<button id="show">显示对话框</button>
  
<!-- script -->      
<script>
    var dialog = document.querySelector("dialog");
            
    document.querySelector("#show").onclick = function(){
        dialog.show();
    };
            
    document.querySelector("#close").onclick = function(){
        dialog.close();
    };
</script>

 

你能够传递一个参数给dialog.close()。经过监听dialog元素的close事件,dialog.returnValue属性将返回给定的值。

如:

<!--HTML-->
<dialog>
    <p>这是dialog对话框!</p>
    <p><input type="text" id="return_value" value="" placeholder="请输入内容"/></p>
    <button id="close">关闭对话框</button>
</dialog>
<button id="show">显示对话框</button>

<!--script-->
<script>
    var dialog = document.querySelector("dialog");
    
    document.querySelector("#show").onclick = function(){
        dialog.showModal();
    };
    
    document.querySelector("#close").onclick = function(){
        var val = document.querySelector("#return_value").value;
        dialog.close(val);
    };
    
    //监听dialog元素的close事件
    dialog.addEventListener("close", function(){
        alert(this.returnValue);
    });
</script>

 

显示dialog对话框的另外一个api是.showModal()

若是你不但愿用户与对话框之外的其余页面元素对象进行交互,那么请使用.showModal()打开对话框而不是使用.show().showModal()打开的对话框会有一个全窗口的半透明背景层,阻断用户与对话框以外的页面元素对象进行交互,同时对话框会默认显示在窗口正中间(上下左右都居中);而用.show()打开的对话框会默认显示在窗口顶部(能够经过css实现居中显示)。

关闭对话框后,close会触发一个事件。另外,用户能够经过输入“Escape”键来关闭模式对话框。这将激发cancel您能够取消使用事件event.preventDefault()

 

 三、与表单集成使用

您可使用form[method="dialog"]将表单与一个<dialog>元素集成使用表单提交后,它会关闭对话框并设置dialog.returnValuevalue已使用的提交按钮。

此外,您可使用该autofocus属性在弹出对话框时自动将焦点对准对话框内的窗体控件。

例如:

<!--HTML-->
<dialog id ="dialog">
    <form method ="dialog">
        <p>你是否赞成使用条款?</p>
        <p><textarea class ="form-control" disabled>条款要求...</textarea></p>
        <button type ="submit" value ="是"></button>
        <button type ="submit" value ="否" autofocus></button>
    </form>
</dialog>
<button id="show">显示表单对话框</button>

<!--script-->
<script>
    var dialog = document.querySelector("dialog");
    
    document.querySelector("#show").onclick = function(){
        dialog.showModal();
    };
    
    //监听dialog元素的close事件
    dialog.addEventListener("close", function(e){
        if(this.returnValue === ""){
            alert(this.returnValue)
            //dosomething...
        }else{
            alert(this.returnValue)
            //dosomething...
        };
    });
</script>

 

 4、浏览器兼容性

桌面浏览器只有谷歌浏览器支持dialog的完整功能(到本博文发表时),要实现跨浏览器兼容请使用dialog-polyfill。

 

参考文章:对话框元素演示

相关文章
相关标签/搜索