JavaScript实现遮罩层

时间:2021-11-02 06:41:30
 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.bg{
border:1px solid black;
display:none;
position:fixed;
width:100%;
height:100%;
background:#000;
z-index:2;
top:0;
left:0;
opacity:0.5;
}
.content{
display:none;
width:500px;
height:300px;
position:fixed;
top:50%;
margin-top:-150px;
background:#fff;
z-index:3;
left:50%;
margin-left:-250px;}
</style>
</head>
<body>
<div class="click">点击这里</div>
<div class="bg"></div>
<div class="content">这里是正文内容</div> <script src="js/jquery-1.11.0.min.js"></script>
<script>
$(function(){
$('.click').click(function(){
$('.bg').css({'display':'block'});
$('.content').css({'display':'block'});
});
$('.bg').click(function(){
$('.bg').css({'display':'none'});
$('.content').css({'display':'none'});
});
});
</script>
</body>
</html>

运行结果

JavaScript实现遮罩层

JavaScript实现遮罩层