0

我试图制作一种灯箱思想。它是一种学习体验,所以我不想使用 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;
}
4

2 回答 2

1
  1. 你不需要保持状态,因为如果覆盖在这里你不能点击不正确的按钮,
  2. 您使用了选择器,而您只需要制作boxvar 一次,
  3. 当您使用一个叠加元素时,您需要每次都为它绑定点击事件,(见one()我用
  4. 最好使用$(document).ready(function() {})整个代码来保持全局范围的清洁。
  5. 我做了一个技巧,只绑定一个元素的点击并由另一个元素触发它,没有重复的事件运行器。

这里是固定代码

$(document).ready(function() {
    
function startOverlay(box) {
    $(".pageOverlay").css({
        "opacity": "0.85"
    });
    $(".pageOverlay").fadeIn("fast");
    box.fadeIn("fast");
}

//closes the overlay and box


function closeBox(box) {
    $(".pageOverlay").fadeOut("fast");
    box.fadeOut("fast");
}

function centerBox(box) {
    //alert(id);
    var browserWidth = document.documentElement.clientWidth;
    var browserHeight = document.documentElement.clientHeight;
    var boxHeight = box.height();
    var boxWidth = box.width();

    box.css({
        "position": "absolute",
        "top": (browserHeight / 2) - (boxHeight / 2),
        "left": (browserWidth / 2) - (boxWidth / 2)
    });

}
 //trigger click on close button by click on pageOverlay, to not duplicate events
 $(".pageOverlay").bind("click", function() {
     $('.closeBtn:visible').click();
        });
    
    
function runBox(link, id) {
    var box = $(id);
    $(link).click(function() {
        startOverlay(box);
        centerBox(box);
        //this function closes the overlay when user clicks "close" or outside of area
        //bind to only visible button
        $(".closeBtn:visible").one("click",function() {
            closeBox(box);
        })

    });


}

    runBox("#div0", "#box0");
    runBox("#div1", "#box1");
});​

我还可以建议生成对话框内容,而不是在 DOM 中包含多个元素

于 2012-12-01T23:47:21.843 回答
0

我认为问题出在绑定事件处理程序时:如果您尝试这样做:

//this function closes the overlay when user clicks outside the message area
$(".pageOverlay").click(function(){
    alert(id); //Or better console.log(id)
    closeBox(id);
});

您将看到任何链接都会触发函数 closeBox两次,这是因为您实际上是两次调用事件绑定。尝试消除该runBox()功能:

//Makes sure the document is fully loaded. 
$(document).ready(function(){

    $('#div0').click(function(){
        startOverlay('#box0'); 
        centerBox('#box0');
    });

    $('#div1').click(function(){
        startOverlay('#box1'); 
        centerBox('#box1');
    });

    //this function closes the overlay when user clicks "close"
    //Note: you can bind both on the same call
    $(".closeBtn,.pageOverlay").click(function(){
        if(jQuery('#box0:visible').length>0){
            closeBox('#box0');
        }else{
            closeBox('#box1');
        }
    });

});​
于 2012-12-02T00:02:10.357 回答