我的问题是我第一次看到计时器下降,但在那之后它变得很奇怪,最后浏览器崩溃了。
我把给我错误的代码放在这里,这是一个巨大的东西,所以我不能把它全部放在这里。
JavaScript:
// display the lightbox
function lightbox(insertContent) {
// jQuery wrapper (optional, for compatibility only)
(function($) {
// add lightbox/shadow <div/>'s if not previously added
if ($('#lightbox').size() === 0) {
var theLightbox = $('<div id="lightbox"/>');
var theShadow = $('<div id="lightbox-shadow"/>');
var countDown = $('<div class="countDown"/>');
$(theShadow).click(function(e) {
closeLightbox();
});
$('body').append(theShadow);
$('body').append(theLightbox);
$('body').append(countDown);
}
// insert HTML content
if (insertContent !== null) {
$('#lightbox').html(insertContent);
$('#lightbox').corner("15px");
// ALWAYS LAST
//$('#lightbox').append(countDown);
CountDown(5);
}
// move the lightbox to the current window top + 100px
$('#lightbox').css('top', $(window).scrollTop() + 100 + 'px');
$('#lightbox-shadow').css('top', $(window).scrollTop());
$('.countDown').css('top', $(window).scrollTop() + 150 + "px");
// display the lightbox
$('#lightbox').show();
$('#lightbox-shadow').show();
$('.countDown').show();
})(jQuery); // end jQuery wrapper
}
function CountDown(tiempo) {
if (tiempo <= 0) {
clearInterval(IntervalID);
closeLightbox();
} else {
$(".countDown").html("Esta ventana se cerrará en " + tiempo + " segundos");
tiempo--;
}
var IntervalID = setInterval("CountDown(" + tiempo + ")", 1000);
}
// close the lightbox
function closeLightbox() {
// jQuery wrapper (optional, for compatibility only)
(function($) {
// hide lightbox/shadow <div/>'s
$('#lightbox').hide();
$('#lightbox-shadow').hide();
$('.countDown').hide();
// remove contents of lightbox in case a video or other content is actively playing
$('#lightbox').empty();
})(jQuery); // end jQuery wrapper
};
$(document).ready(function() {
$("#Login").click(function(event) {
event.preventDefault();
lightbox("Username Not Available");
$("#lightbox").css("color", "#FF0000");
});
});
HTML:
<input type="button" id="Login" value="Hello">
CSS:
#lightbox {
position: absolute;
width: 50%;
left: 25%;
background: #fff;
z-index: 1001;
display: none;
color: #069;
padding: 20px;
text-align: center;
font-size: 24px;
font-weight: bold;
font-variant: small-caps;
text-shadow: 1px 1px #000;
}
#lightbox-shadow {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #000;
filter: alpha(opacity=90);
-moz-opacity: 0.90;
-khtml-opacity: 0.90;
opacity: 0.90;
z-index: 1000;
display: none;
}
.countDown {
position: absolute;
width: 50%;
left: 25%;
background: #fff;
z-index: 1002;
display: none;
color: #069;
padding: 20px;
text-align: center;
font-size: 18px;
font-variant: small-caps;
font-weight: normal;
margin: 10px auto;
display: none;
}
JsFiddle:
不知道为什么它在 jsfiddle 上不起作用;它在我的电脑上运行良好。