<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Modal</title>
<style>
#btn {
margin: 10px;
}
.overlay {
display: none;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
height: 100%;
width: 100%;
background: rgba(0, 0, 0, 0.5);
z-index: 99;
}
.modal {
width: 500px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border: 1px solid #ccc;
border-radius: 6px;
background-color: #fff;
}
.modal>h2 {
border-bottom: 1px solid #ccc;
margin: 0;
line-height: 2;
padding-left: 20px;
}
.modal>.detail {
padding: 0 20px;
height: 100px;
border-bottom: 1px solid #ccc;
}
.modal>.btn {
line-height: 2.2;
cursor: pointer;
float: right;
margin: 0 10px;
}
</style>
</head>
<body>
<input id="btn" type="button" value="点我">
<div class="overlay">
<div class="modal">
<h2>我是标题</h2>
<div class="detail">
<p>我是内容</p>
<p>我是内容</p>
</div>
<span class="btn close">确定</span>
<span class="btn cancel">取消</span>
</div>
</div>
<script>
var btn = document.getElementById('btn'),
overlay = document.getElementsByClassName('overlay')[0],
modal = document.getElementsByClassName('modal')[0];
btn.addEventListener('click',function(e){
e.stopPropagation();
overlay.setAttribute('style','display:block');
});
document.body.addEventListener('click',function(e){
overlay.setAttribute('style','display:none');
});
modal.addEventListener('click',function(e){
e.stopPropagation();
if(e.target.classList.contains('close') || e.target.classList.contains('cancel')){
overlay.setAttribute('style','display:none');
}
});
</script>
</body>
</html>
自制弹窗页面
最新推荐文章于 2023-09-09 21:31:37 发布