js模態(tài)對(duì)話框使用方法詳解
模態(tài)框(Modal Dialogue Box)也可叫做模態(tài)對(duì)話框,或者對(duì)話框,當(dāng)一個(gè)模態(tài)框被打開時(shí),用戶可以與該對(duì)話框進(jìn)行交互,點(diǎn)擊關(guān)閉按鈕可關(guān)閉該模態(tài)框!
功能實(shí)現(xiàn):
1. 頁(yè)面上有一個(gè)按鈕,用于打開模態(tài)框,模態(tài)框默認(rèn)隱藏;
2. 用戶點(diǎn)擊按鈕,可打開模態(tài)框;用戶點(diǎn)擊模態(tài)框中的關(guān)閉或者點(diǎn)擊頁(yè)面其他地方可關(guān)閉該模態(tài)框
✦ 點(diǎn)擊頁(yè)面其他地方,關(guān)閉模態(tài)框,可用window.onclick事件
✦ 給關(guān)閉按鈕綁定點(diǎn)擊事件,按鈕被點(diǎn)擊,模態(tài)框Modal添加樣式display:none;
✦ 給button按鈕綁定點(diǎn)擊事件,當(dāng)按鈕被點(diǎn)擊時(shí),模態(tài)框Modal添加樣式display:block;
✦ 先獲取頁(yè)面上的button按鈕,關(guān)閉按鈕,以及模態(tài)框Modal
代碼實(shí)現(xiàn):
<html>
<head>
<style>
/* 彈窗 (background) */
.modal {
display: none; /* 默認(rèn)隱藏 */
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.4);
}
/* 彈窗內(nèi)容 */
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 1px solid #888;
width: 35%;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
-webkit-animation-name: animatetop;
-webkit-animation-duration: 0.4s;
animation-name: animatetop;
animation-duration: 0.4s
}
/* 添加動(dòng)畫 */
@-webkit-keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
@keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
/* 關(guān)閉按鈕 */
.close {
color: white;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.modal-header {
padding: 2px 16px;
background-color: #5587A2;
color: white;
}
.modal-body {padding: 2px 16px;}
.modal-footer {
padding: 2px 16px;
background-color: #5587A2;
text-align: right;
color: white;
}
</style>
</head>
<body>
<!-- 打開彈窗按鈕 -->
<button id="myBtn">打開彈窗</button>
<!-- 彈窗 -->
<div id="myModal" class="modal">
<!-- 彈窗內(nèi)容 -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<h2>彈窗頭部</h2>
</div>
<div class="modal-body">
<p>彈窗內(nèi)容...</p>
<p>彈窗內(nèi)容...</p>
</div>
<div class="modal-footer">
<h3>彈窗底部</h3>
</div>
</div>
</div>
<script>
// 獲取彈窗
var modal = document.getElementById('myModal');
// 打開彈窗的按鈕對(duì)象
var btn = document.getElementById("myBtn");
// 獲取 <span> 元素,用于關(guān)閉彈窗 that closes the modal
var span = document.getElementsByClassName("close")[0];
// 點(diǎn)擊按鈕打開彈窗
btn.onclick = function() {
modal.style.display = "block";
}
// 點(diǎn)擊 <span> (x), 關(guān)閉彈窗
span.onclick = function() {
modal.style.display = "none";
}
// 在用戶點(diǎn)擊其他地方時(shí),關(guān)閉彈窗
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JS 模態(tài)對(duì)話框和非模態(tài)對(duì)話框操作技巧匯總
- 利用javascript打開模態(tài)對(duì)話框(示例代碼)
- javascript showModalDialog模態(tài)對(duì)話框使用說(shuō)明
- JavaScript 實(shí)現(xiàn)模態(tài)對(duì)話框 源代碼大全
- 詳解AngularJS 模態(tài)對(duì)話框
- JS對(duì)話框_JS模態(tài)對(duì)話框showModalDialog用法總結(jié)
- 兩種WEB下的模態(tài)對(duì)話框 (asp.net或js的分別實(shí)現(xiàn))
- js實(shí)現(xiàn)div模擬模態(tài)對(duì)話框展現(xiàn)URL內(nèi)容
- ModelDialog JavaScript模態(tài)對(duì)話框類代碼
- JavaScript實(shí)現(xiàn)模態(tài)對(duì)話框?qū)嵗?/a>
- js實(shí)現(xiàn)響應(yīng)按鈕點(diǎn)擊彈出可拖拽的非模態(tài)對(duì)話框完整實(shí)例【測(cè)試可用】
相關(guān)文章
JavaScript實(shí)現(xiàn)放大鏡效果代碼示例
這篇文章主要介紹了JavaScript實(shí)現(xiàn)放大鏡效果代碼示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Bootstrap實(shí)現(xiàn)基于carousel.js框架的輪播圖效果
這篇文章主要為大家詳細(xì)介紹了Bootstrap實(shí)現(xiàn)基于carousel.js框架的輪播圖效果,無(wú)過(guò)渡動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
一個(gè)php+js實(shí)時(shí)顯示時(shí)間問(wèn)題
本文給大家分享的是解決的php+js實(shí)時(shí)顯示時(shí)間問(wèn)題,主要是自己當(dāng)時(shí)的理解有問(wèn)題,也許大家有和我一樣的情況,這里分享給大家2015-10-10
基于Unit PNG Fix.js有時(shí)候在ie6下不正常的解決辦法
本篇文章是對(duì)Unit PNG Fix.js有時(shí)候在ie6下不正常的解決辦法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
js鼠標(biāo)滑過(guò)彈出層的定位IE6bug解決辦法
大家在寫div+css的時(shí)候經(jīng)常會(huì)用到彈出層,由于IE6的bug,所以當(dāng)使用多個(gè)標(biāo)簽重復(fù)寫彈出層的時(shí)候會(huì)遇到后面的層壓在了彈出層的上面;用Jquery給彈出層的z軸依次增加高度可解決.代碼很簡(jiǎn)單,效果很顯著,需要了解的朋友可以參考下2012-12-12
讓JavaScript 輕松支持函數(shù)重載 (Part 1 - 設(shè)計(jì))
JavaScript支持函數(shù)重載嗎?可以說(shuō)不支持,也可以說(shuō)支持。說(shuō)不支持,是因?yàn)镴avaScript不能好像其它原生支持函數(shù)重載的語(yǔ)言一樣,直接寫多個(gè)同名函數(shù),讓編譯器來(lái)判斷某個(gè)調(diào)用對(duì)應(yīng)的是哪一個(gè)重載。2009-08-08
原生JS實(shí)現(xiàn)簡(jiǎn)單放大鏡效果
這篇文章主要為大家詳細(xì)介紹了原生JS實(shí)現(xiàn)簡(jiǎn)單放大鏡效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02

