首页 > 代码库 > 点击空白处隐藏盒子
点击空白处隐藏盒子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>点击空白处隐藏盒子</title>
<style>
#mask{
width: 100%;
height:2000px;
font-weight:bold;">#000;
opacity: 0.4;
filter:alpha(opacity = 40);
position: fixed;
top:0;
left: 0;
display: none;
}
body{
height: 2000px;
}
#box{
width: 300px;
height: 300px;
font-weight:bold;">#fff;
position: absolute;
left: 50%;
top: 50%;
margin-left: -150px;
margin-top: -150px;
display: none;
}
</style>
</head>
<body>
<a href="javascript:;" id="login">注册</a>
<a href="javascript:;">登录</a>
<div id="mask"></div>
<div id="box"></div>
</body>
<script>
// var function $(id){return document.getElementById(id)};
var login = document.getElementById("login");
var mask = document.getElementById("mask");
var box = document.getElementById("box")
//阻止冒泡
login.onclick = function(event){
var event = event||window.event;
if(event && event.stopPropagation){
event.stopPropagation();
}else{
event.cancelBubble = true;
}
mask.style.display = "block";
box.style.display = "block";
document.body.style.overflow = "hidden";
}
document.onclick = function(event){
var event = event || window.event;
//判断当前对象是否是自己
var targetId = event.target ? event.target.id : event.srcElement.id; //兼容写法
if(targetId != "box"){
mask.style.display = "none";
box.style.display = "none";
document.body.style.overflow = "visible";
}else{
mask.style.display = "block";
box.style.display = "block";
document.body.style.overflow = "hidden";
}
}
</script>
</html>
点击空白处隐藏盒子
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。