<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>纯JS实现弹出框</title>
<style type="text/css">
/*弹出层*/
.popPosition {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
#pop-container {
display: none;
z-index: 999;
}
/*遮盖层*/
#cover-tier {
background-color: #D9D9D9;
opacity: 0.5;
}
/*弹出层内容*/
#pop-tier {
width: 100px;
height: 50px;
border: 5px solid #C3C3C3;
background-color: #fff;
margin: auto;
text-align: center;
}
#pop-tier>p {
margin-top: 10px;
}
</style>
</head>
<body>
<button id="btn">点击弹出层</button>
<div id="pop-container">
<div id="cover-tier"></div>
<div id="pop-tier">
<p id="alert">弹出框内容</p>
</div>
</div>
<script type="text/javascript">
function temp(id) {
return document.getElementById(id);
}
// 点击按钮出现弹出层
temp('btn').onclick = function() {
//temp("alert").innerHTML = msg;
temp('pop-container').style.display = 'block';
};
// 弹出层隐藏
temp('pop-tier').onclick = function() {
temp('pop-container').style.display = 'none';
};
temp('cover-tier').onclick = function() {
temp('pop-container').style.display = 'none';
};
</script>
</body>
</html>
暂无评论