内容简介:HTML 5.2草案加入了新的dialog元素。但是是一种实验技术。以前,如果我们想要构建任何形式的模式对话框或对话框,我们需要有一个背景,一个关闭按钮,将事件绑定在对话框中的方式安排我们的标记,找到一种将消息传递出去的方式对话……这真的很复杂。对话框元素解决了上述所有问题。下面是一个bootstrap模态框的html结构:
使用HTML5原生对话框元素,轻松创建模态框组件
HTML 5.2草案加入了新的dialog元素。但是是一种实验技术。
以前,如果我们想要构建任何形式的模式对话框或对话框,我们需要有一个背景,一个关闭按钮,将事件绑定在对话框中的方式安排我们的标记,找到一种将消息传递出去的方式对话……这真的很复杂。对话框元素解决了上述所有问题。
Bootstrap 模态框和原生模态框的对比
下面是一个bootstrap模态框的html结构:
<!-- 按钮触发模态框 --> <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"> × </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结构:
<!-- 按钮触发模态框 --> <button type="button" class="btn">显示模态框</button> <!-- 模态框 --> <dialog open> HTML5原生模态框 </dialog>
基础的模态框样式
我们已经看到了对话框元素的最简单标记,您可能已经注意到open是上面对话框中的属性。将该属性添加到元素将强制显示对话框,否则将删除它。该对话框也将绝对定位在页面上。
上图展示了一个最基本的模态框样式。
打开浏览器可以查看到它的基本样式是这样的:
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样式如下:
dialog::backdrop { position: fixed; top: 0px; right: 0px; bottom: 0px; left: 0px; background: rgba(0, 0, 0, 0.1); }
设置对话框样式
我们可以像任何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”]将表单与一个
此外,您可以使用该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>
浏览器兼容性
桌面浏览器只有谷歌浏览器支持dialog的完整功能(到本博文发表时),要实现跨浏览器兼容请使用 dialog-polyfill 。
以上所述就是小编给大家介绍的《使用HTML5原生对话框元素,轻松创建模态框组件》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 如何使用JavaScript构建模态框插件
- 自定义带动画效果的模态框
- Angluar-模态视图构建简析(A)
- React 30 秒速学:制作模态框组件
- BERT在多模态领域中的应用
- 使用React手写一个对话框或模态框
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Designing Data-Intensive Applications
Martin Kleppmann / O'Reilly Media / 2017-4-2 / USD 44.99
Data is at the center of many challenges in system design today. Difficult issues need to be figured out, such as scalability, consistency, reliability, efficiency, and maintainability. In addition, w......一起来看看 《Designing Data-Intensive Applications》 这本书的介绍吧!