gpt4 book ai didi

jquery - 如何将 10 秒倒数计时器添加到弹出窗口

转载 作者:行者123 更新时间:2023-11-27 23:53:58 24 4
gpt4 key购买 nike

我只想向弹出窗口添加一个 10 秒的倒数计时器,之后弹出窗口将关闭。

加载弹出窗口后,我需要在弹出窗口中显示 10 秒倒计时。一旦 10 秒过去,弹出窗口应该关闭。

这是我到目前为止尝试过的代码:

<html>
<head>
<title>Popup Box DIV</title>
<style type="text/css">
#popup_box {
display: none; /* Hide the DIV */
position: fixed;
_position: absolute; /* hack for internet explorer 6 */
height: 500px;
width: 500px;
background:;
left: 400px;
top: 50px;
z-index: 100; /* Layering ( on-top of others), if you have lots of layers: I just maximized, you can change it yourself */
margin-left: 25px;
/* additional features, can be omitted */
border: 2px;
padding: 15px;
font-size: 15px;
-moz-box-shadow: 0 0 5px;
-webkit-box-shadow: 0 0 5px;
box-shadow: 0 0 5px;
}
#container {
background:; /*Sample*/
width: 100%;
height: 100%;
}
a {
cursor: pointer;
text-decoration: none;
}
/* This is for the positioning of the Close Link */
#popupBoxClose {
font-size: 20px;
line-height: 15px;
right: 5px;
top: 5px;
position: absolute;
color: #6fa5e2;
font-weight: 500;
}
</style>
<script src="jquery-1.2.6.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready( function() {

// When site loaded, load the Popupbox First
loadPopupBox();

$('#popupBoxClose').click( function() {
unloadPopupBox();
});

$('#container').click( function() {
unloadPopupBox();
});

function unloadPopupBox() { // TO Unload the Popupbox
$('#popup_box').fadeOut("slow");
$("#container").css({ // this is just for style
"opacity": "1"
});
}

function loadPopupBox() { // To Load the Popupbox
$('#popup_box').fadeIn("slow");
$("#container").css({ // this is just for style
"opacity": "0.3"
});
}
});
</script>
</head>
<body>
<div id="popup_box"> <!-- OUR PopupBox DIV-->
<h1>it closed after 10 sec</h1>
<a id="popupBoxClose">Close</a> </div>
<div id="container"> <!-- Main Page -->
<h1></h1>
</div>
</body>
</html>

最佳答案

您需要使用setInterval。这将每隔 x 时间触发一个函数(在本例中为 1000 毫秒或 1 秒)。此函数负责减少计数器变量,直到它达到 0,此时它卸载弹出窗口并使用 clearInterval 停止间隔再次运行。

http://jsfiddle.net/chrissp26/8ygva6hm/

像这样给 H1 添加一个 ID:

<h1 id="countDown">it closed  after 10 sec</h1>

并将您的 JavaScript 更改为:

$(document).ready( function() {

// When site loaded, load the Popupbox First
loadPopupBox();

$('#popupBoxClose').click( function() {
unloadPopupBox();
});

$('#container').click( function() {
unloadPopupBox();
});

function unloadPopupBox() { // TO Unload the Popupbox
$('#popup_box').fadeOut("slow");
$("#container").css({ // this is just for style
"opacity": "1"
});
}

function loadPopupBox() { // To Load the Popupbox

var counter = 10;
var id;
$('#popup_box').fadeIn("slow");
$("#container").css({ // this is just for style
"opacity": "0.3"
});

id = setInterval(function() {
counter--;
if(counter < 0) {
clearInterval(id);

unloadPopupBox();
} else {
$("#countDown").text("it closed after " + counter.toString() + " seconds.");
}
}, 1000);

}
});

编辑:版本 2

这里有一个您可能会喜欢的不同版本。该脚本是完全封装的,因此更加整洁。它还具有更平滑的动画过渡等。

http://jsfiddle.net/chrissp26/8ygva6hm/2/

关于jquery - 如何将 10 秒倒数计时器添加到弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25197035/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com