教你使用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。

 

參考文章:對話框元素演示

相關文章
相關標籤/搜索