<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.clearfix:after {
content: "";
display: block;
clear: both;
}
#btn {
display: block;
font-size: 1.5rem;
padding: 5px;
margin: 0 auto;
cursor: pointer;
}
.panel {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 10px;
width: 300px;
border: 1px solid;
border-radius: 5px;
background: #fff;
display: none;
z-index: 2;
}
.panel .close {
float: right;
margin-top: 15px;
margin-right: 15px;
font-size: 1.15rem;
cursor: pointer;
}
.panel .sure,
.panel .cancel {
border: 1px solid;
border-radius: 5px;
margin: 5px;
padding: 5px;
float: right;
cursor: pointer;
}
.shadow {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #ccc;
opacity: 0.75;
z-index: 1;
display: none;
}
</style>
</head>
<body>
<button id="btn">點(diǎn)我跳出模態(tài)框</button>
<div class="panel clearfix">
<div class="close">X</div>
<h1>我是一級(jí)標(biāo)題</h1>
<p>我是一大段文字</p>
<div class="cancel">取消</div>
<div class="sure">確定</div>
</div>
<div class="shadow"></div>
<script>
function $(str) {
return document.querySelector(str);
}
var panel = $(".panel");
var btn = $("#btn");
var close = $(".panel>.close")
var shadow = $(".shadow")
btn.addEventListener("click", function(e) {
e.stopPropagation();
panel.style.display = "block";
shadow.style.display = "block";
})
close.addEventListener("click", function() {
panel.style.display = "none";
shadow.style.display = "none";
})
panel.addEventListener("click", function(e) {
e.stopPropagation();
})
window.addEventListener("click", function() {
panel.style.display = "none";
shadow.style.display = "none";
})
</script>
</body>
</html>
原生JS實(shí)現(xiàn)彈出、關(guān)閉模態(tài)框
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- Tap效果 代碼: Tap效果預(yù)覽 模態(tài)框效果 代碼: 模態(tài)框效果預(yù)覽
- 在網(wǎng)頁(yè)中我們經(jīng)常會(huì)用到模態(tài)框,一般會(huì)用于顯示表單或者是提示信息。由于模態(tài)框涉及到頁(yè)面上比較多的交互效果,最簡(jiǎn)單的交...
- 在pc端開(kāi)發(fā),模態(tài)框是一個(gè)很常用的插件,之前一直用的第三方插件,比如bootstrap,jQuery的模態(tài)框插件,...
- JS常見(jiàn)的彈出框(非常之簡(jiǎn)單) 本文適合初入前端的同學(xué)相信不少同學(xué)在學(xué)習(xí)前端的過(guò)程中,不管是制作demo,還是真實(shí)...
- 使用純 CSS 實(shí)現(xiàn) 500px 照片列表布局 文章很長(zhǎng),因?yàn)榻榻B了如何一步一步進(jìn)化到最后接近完美的效果的,不想讀...