我试图制作一种灯箱思想。它是一种学习体验,所以我不想使用 fancybox 等。
我的问题是:我有 2 个链接。每个都打开一个叠加层,顶部是一个带有内容的白框。这行得通。当我关闭时,只有第一个打开白盒的链接会同时关闭白盒和覆盖。第二个链接只关闭覆盖,而不是白盒。:S 怎么来的?
html代码:
<!DOCTYPE>
<head>
<title>Box</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="whitebox.js"></script>
</head>
<body>
<div id="div0">
box0
</div>
<div id="box0" class="box">
<a class="closeBtn">X</a>
<h1>box0</h1>
<form action="" method="POST">
<div>Your email: <input type="text"></div>
<br />
<div>Message: <input type="textarea"></div>
</form>
</div>
<div id="div1">
box1
</div>
<div id="box1" class="box">
<a class="closeBtn">X</a>
<h1>box1</h1>
<form action="" method="POST">
<div>Your email: <input type="text"></div>
<br />
<div>Message: <input type="textarea"></div>
</form>
</div>
<div class="pageOverlay"></div>
</body>
</html>
白盒.js:
var state = 0;
function startOverlay(id){
if(state == 0){
$(".pageOverlay").css({"opacity": "0.85"});
$(".pageOverlay").fadeIn("fast");
$(id).fadeIn("fast");
state = 1;
}
}
//closes the overlay and box
function closeBox(id){
if(state == 1){
$(".pageOverlay").fadeOut("fast");
$(id).fadeOut("fast");
state = 0;
}
}
function centerBox(id){
//alert(id);
var browserWidth = document.documentElement.clientWidth;
var browserHeight = document.documentElement.clientHeight;
var boxHeight = $(id).height();
var boxWidth = $(id).width();
$(id).css({"position": "absolute","top": (browserHeight / 2) - (boxHeight / 2),"left": (browserWidth / 2) - (boxWidth / 2)});
}
function runBox(link, id){
$(link).click(function(){
startOverlay(id);
centerBox(id);
});
//this function closes the overlay when user clicks "close"
$(".closeBtn").click(function(){
closeBox(id);
});
//this function closes the overlay when user clicks outside the message area
$(".pageOverlay").click(function(){
closeBox(id);
});
}
//Makes sure the document is fully loaded.
$(document).ready(function(){
runBox("#div0","#box0");
runBox("#div1","#box1");
});
样式.css:
#div0{
margin-top:150px;
margin-left:auto;
margin-right:auto;
text-align:center;
border:1px solid black;
cursor: pointer;
width: 150px;
}
#div1{
margin-top:150px;
margin-left:auto;
margin-right:auto;
text-align:center;
border:1px solid black;
cursor: pointer;
width: 150px;
}
.pageOverlay{
display:none;
position:fixed;
height:100%;
width:100%;
top:0;
left:0;
background:#000000;
z-index:1000;
}
.box{
display:none;
position:relative;
height:450px;
width:450px;
background:#ffffff;
border:2px solid #a0a0a0;
z-index:2000;
padding:12px;
font-size:14px;
font-family:Arial, Helvetica, sans-serif;
}
.box h1{
text-align:left;
color:#7FA5FE;
font-size:22px;
font-weight:700;
border-bottom:1px solid #a0a0a0;
padding-bottom:2px;
margin-bottom:20px;
font-family:Arial, Helvetica, sans-serif;
}
.box a{
cursor: pointer;
}
.box p{
text-align:center;
}
.closeBtn{
font-size:12px;
line-height:14px;
right:6px;
top:4px;
position:absolute;
color:#7FA5FE;
font-weight:700;
display:block;
#div0{
margin-top:150px;
margin-left:auto;
margin-right:auto;
text-align:center;
border:1px solid black;
cursor: pointer;
width: 150px;
}
#div1{
margin-top:150px;
margin-left:auto;
margin-right:auto;
text-align:center;
border:1px solid black;
cursor: pointer;
width: 150px;
}
.pageOverlay{
display:none;
position:fixed;
height:100%;
width:100%;
top:0;
left:0;
background:#000000;
z-index:1000;
}
.box{
display:none;
position:relative;
height:450px;
width:450px;
background:#ffffff;
border:2px solid #a0a0a0;
z-index:2000;
padding:12px;
font-size:14px;
font-family:Arial, Helvetica, sans-serif;
}
.box h1{
text-align:left;
color:#7FA5FE;
font-size:22px;
font-weight:700;
border-bottom:1px solid #a0a0a0;
padding-bottom:2px;
margin-bottom:20px;
font-family:Arial, Helvetica, sans-serif;
}
.box a{
cursor: pointer;
}
.box p{
text-align:center;
}
.closeBtn{
font-size:12px;
line-height:14px;
right:6px;
top:4px;
position:absolute;
color:#7FA5FE;
font-weight:700;
display:block;
}