JavaScript蒙板(model)功能的簡單實(shí)現(xiàn)代碼
思路:
•創(chuàng)建一個(gè)蒙板, 設(shè)置蒙板的堆疊順序保證能將其它元素蓋住
position: absolute;
top: 0;
left: 0;
display: none;
background-color: rgba(9, 9, 9, 0.63);
width: 100%;
height: 100%;
z-index: 1000; •設(shè)置蒙板中內(nèi)容的背景色和展示格式
width: 50%;
text-align: center;
background: #ffffff;
border-radius: 6px;
margin: 100px auto;
line-height: 30px;
z-index: 10001; •綁定事件, 動(dòng)態(tài)切換蒙板的 display 屬性
function showModel() {
document.getElementById('myModel').style.display = 'block';
}
function closeModel() {
document.getElementById('myModel').style.display = 'none';
}
注意事項(xiàng): 蒙板要采用絕對(duì)定位, 寬和高要占満整個(gè)頁面, 堆疊順序要大
源代碼
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>蒙板</title>
<style>
.container {
width: 900px;
margin: 50px auto;
text-align: center;
}
#myModel {
position: absolute;
top: 0;
left: 0;
display: none;
background-color: rgba(9, 9, 9, 0.63);
width: 100%;
height: 100%;
z-index: 1000;
}
.model-content {
width: 50%;
text-align: center;
background: #ffffff;
border-radius: 6px;
margin: 100px auto;
line-height: 30px;
z-index: 10001;
}
</style>
</head>
<body>
<div class="container">
<button onclick="showModel()">彈出蒙板</button>
<div id="myModel" onclick="closeModel()">
<div class="model-content">
<p>你好啊,我是內(nèi)容~~</p>
<p>
<button id="closeModel" onclick="closeModel()">關(guān)閉</button>
</p>
</div>
</div>
</div>
<script>
function showModel() {
document.getElementById('myModel').style.display = 'block';
}
function closeModel() {
document.getElementById('myModel').style.display = 'none';
}
</script>
</body>
</html>
以上所述是小編給大家介紹的JavaScript蒙板(model)功能的簡單實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Javascript運(yùn)行機(jī)制之Event Loop
這篇文章主要介紹了Javascript運(yùn)行機(jī)制之Event Loop,在學(xué)習(xí)Event Loop前,首先需要了解的幾個(gè)概念Javascript是單線程、任務(wù)隊(duì)列、同步任務(wù)、異步任務(wù)、Javascript執(zhí)行棧,下面來看看文章的詳細(xì)介紹吧2021-12-12
區(qū)別JavaScript函數(shù)聲明與變量聲明
這篇文章給大家分享了關(guān)于JavaScript中函數(shù)聲明與變量聲明之間的區(qū)別以及相關(guān)知識(shí)點(diǎn),有興趣的朋友參考下。2018-09-09
基于Bootstrap實(shí)現(xiàn)的下拉菜單手機(jī)端不能選擇菜單項(xiàng)的原因附解決辦法
小編使用bootstrap做的下拉菜單在電腦瀏覽器中可以正常使用,在手機(jī)瀏覽器中能彈出下拉列表卻不能選擇列表中的菜單項(xiàng),怎么回事,如何解決呢?下面小編給大家分享下具體原因及解決辦法,一起看下吧2016-07-07

