下面我将为您详细讲解“jQuery简单倒计时效果完整示例”的攻略。
1. 初始设置
在HTML代码中,需要先建立一个倒计时容器,并在其中设置好倒计时的初始状态,如下所示:
<div id="countdown">
<div>
<span id="days"></span>
<div class="countdown-text">Days</div>
</div>
<div>
<span id="hours"></span>
<div class="countdown-text">Hours</div>
</div>
<div>
<span id="minutes"></span>
<div class="countdown-text">Minutes</div>
</div>
<div>
<span id="seconds"></span>
<div class="countdown-text">Seconds</div>
</div>
</div>
这段HTML代码会生成一个类似于时钟的倒计时效果,并将倒计时的天数、小时数、分钟数、秒数展示在相应的HTML元素中。
2. 倒计时的实现
在JS代码中,使用jQuery库来获取和操作HTML元素。首先,需要设置好要倒计时的目标时间,并将其转换成JavaScript中的日期对象:
var endDate = new Date("2022-01-01T00:00:00");
然后在倒计时函数中,使用JavaScript日期对象中提供的方法来获取当前时间和目标时间之间的差值,并将差值转换为天数、小时数、分钟数、秒数,最终将这些值更新到HTML元素中:
function countdown() {
var now = new Date();
var timeLeft = endDate - now;
var daysLeft = Math.floor(timeLeft / (1000 * 60 * 60 * 24));
var hoursLeft = Math.floor((timeLeft / (1000 * 60 * 60)) % 24);
var minutesLeft = Math.floor((timeLeft / (1000 * 60)) % 60);
var secondsLeft = Math.floor((timeLeft / 1000) % 60);
$("#days").text(daysLeft.toLocaleString(undefined, {minimumIntegerDigits: 2}));
$("#hours").text(hoursLeft.toLocaleString(undefined, {minimumIntegerDigits: 2}));
$("#minutes").text(minutesLeft.toLocaleString(undefined, {minimumIntegerDigits: 2}));
$("#seconds").text(secondsLeft.toLocaleString(undefined, {minimumIntegerDigits: 2}));
setTimeout(countdown, 1000);
}
在倒计时值的更新中,使用了toLocaleString方法,并设置了参数{minimumIntegerDigits: 2},来确保数字在展示时前导0个数正确。最后,使用setTimeout函数来递归调用countdown函数,以实现倒计时的循环更新。
3. 完整示例说明
下面提供两条完整示例说明,以帮助您更好的理解倒计时效果的实现方法。
示例1:自定义倒计时目标时间
在下面的示例中,使用了自定义的倒计时目标时间(2022-12-31T23:59:59)。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jQuery简单倒计时效果完整示例</title>
<style>
#countdown {
display: flex;
justify-content: space-between;
width: 340px;
margin: 0 auto;
}
#countdown > div {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
font-size: 24px;
}
#countdown > div span {
font-size: 48px;
}
.countdown-text {
text-transform: uppercase;
font-size: 12px;
}
</style>
</head>
<body>
<div id="countdown">
<div>
<span id="days"></span>
<div class="countdown-text">Days</div>
</div>
<div>
<span id="hours"></span>
<div class="countdown-text">Hours</div>
</div>
<div>
<span id="minutes"></span>
<div class="countdown-text">Minutes</div>
</div>
<div>
<span id="seconds"></span>
<div class="countdown-text">Seconds</div>
</div>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
var endDate = new Date("2022-12-31T23:59:59");
function countdown() {
var now = new Date();
var timeLeft = endDate - now;
var daysLeft = Math.floor(timeLeft / (1000 * 60 * 60 * 24));
var hoursLeft = Math.floor((timeLeft / (1000 * 60 * 60)) % 24);
var minutesLeft = Math.floor((timeLeft / (1000 * 60)) % 60);
var secondsLeft = Math.floor((timeLeft / 1000) % 60);
$("#days").text(daysLeft.toLocaleString(undefined, {minimumIntegerDigits: 2}));
$("#hours").text(hoursLeft.toLocaleString(undefined, {minimumIntegerDigits: 2}));
$("#minutes").text(minutesLeft.toLocaleString(undefined, {minimumIntegerDigits: 2}));
$("#seconds").text(secondsLeft.toLocaleString(undefined, {minimumIntegerDigits: 2}));
setTimeout(countdown, 1000);
}
countdown();
</script>
</body>
</html>
示例2:改变倒计时效果样式
在下面的示例中,改变了原先的倒计时效果样式(增加了一个背景颜色和动画效果)。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jQuery简单倒计时效果完整示例</title>
<style>
body {
background-color: #f1f1f1;
}
#countdown {
display: flex;
justify-content: space-between;
width: 340px;
margin: 0 auto;
background-color: #fff;
border-radius: 5px;
padding: 20px;
box-shadow: 0 0 20px rgba(0,0,0,0.2);
}
#countdown > div {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
font-size: 24px;
width: 75px;
height: 100px;
background-color: #eee;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
animation: scale 0.5s ease-in-out;
animation-fill-mode: forwards;
}
@keyframes scale {
0% {
opacity: 0;
transform: scale(0);
}
100% {
opacity: 1;
transform: scale(1);
}
}
#countdown > div span {
font-size: 48px;
}
.countdown-text {
text-transform: uppercase;
font-size: 12px;
}
</style>
</head>
<body>
<div id="countdown">
<div>
<span id="days"></span>
<div class="countdown-text">Days</div>
</div>
<div>
<span id="hours"></span>
<div class="countdown-text">Hours</div>
</div>
<div>
<span id="minutes"></span>
<div class="countdown-text">Minutes</div>
</div>
<div>
<span id="seconds"></span>
<div class="countdown-text">Seconds</div>
</div>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
var endDate = new Date("2022-12-31T23:59:59");
function countdown() {
var now = new Date();
var timeLeft = endDate - now;
var daysLeft = Math.floor(timeLeft / (1000 * 60 * 60 * 24));
var hoursLeft = Math.floor((timeLeft / (1000 * 60 * 60)) % 24);
var minutesLeft = Math.floor((timeLeft / (1000 * 60)) % 60);
var secondsLeft = Math.floor((timeLeft / 1000) % 60);
$("#days").text(daysLeft.toLocaleString(undefined, {minimumIntegerDigits: 2}));
$("#hours").text(hoursLeft.toLocaleString(undefined, {minimumIntegerDigits: 2}));
$("#minutes").text(minutesLeft.toLocaleString(undefined, {minimumIntegerDigits: 2}));
$("#seconds").text(secondsLeft.toLocaleString(undefined, {minimumIntegerDigits: 2}));
setTimeout(countdown, 1000);
}
countdown();
</script>
</body>
</html>
这两个示例代码中,均使用了jQuery库来操作HTML元素,并通过设置不同的endDate来设定不同的倒计时目标时间,实现了完整的倒计时效果,并使用了不同的CSS样式来设置不同的倒计时效果展示方式。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:jQuery简单倒计时效果完整示例 - Python技术站