大家好,今天要给大家分享的是京东首页秒杀效果,用js实现时分秒的倒计时。这个效果在很多网站上都会用到,比较实用,下面是代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
#box {
width: 200px;
height: 300px;
margin: 200px 200px;
background: red;
position: relative;
}
.txt {
width: 150px;
height: 50px;
text-align: center;
line-height: 50px;
color: #fff;
font-size: 30px;
font-weight: 900;
position: absolute;
left: 25px;
top: 50px;
}
.hour {
left: 20px;
}
.h_m {
left: 68px;
}
.minute {
left: 80px;
}
.m_s {
right: 68px;
}
.second {
left: 140px;
}
.hour,
.minute,
.second {
position: absolute;
top: 200px;
color: #fff;
font-weight: 800;
font-size: 20px;
text-align: center;
line-height: 40px;
width: 40px;
height: 40px;
background: black;
}
.h_m,
.m_s {
color: #fff;
font-size: 20px;
font-weight: 900;
position: absolute;
bottom: 70px;
}
.words {
margin-top: 160px;
font-size: 18px;
color: white;
margin-left: 20px;
text-align: center;
position: absolute;
}
</style>
</head>
<body>
<div id="box">
<div class="txt">京东秒杀</div>
<div class="words"><strong>16:00</strong> 点场 倒计时</div>
<div class="hour"></div>
<!-- 小时与分钟之间的冒号 -->
<span class="h_m">:</span>
<div class="minute"></div>
<!-- 分钟与秒之间的冒号 -->
<span class="m_s">:</span>
<div class="second"></div>
</div>
</body>
<script>
//1、获取元素
var hour = document.querySelector(".hour");
var minute = document.querySelector(".minute");
var second = document.querySelector(".second");
var inputTime = +new Date("2020-10-16 18:00:00");//倒计时的结束时间,自己设置时间
countDown();//先调用一次这个函数 防止第一次刷新页面有空白
//2、开启定时器
setInterval(countDown, 1000);//1000毫秒,每一秒钟调用一次函数
//3、倒计时-时分秒函数
function countDown() {
var nowTime = +new Date(); //返回的是当前时间的总的毫秒数
var times = (inputTime - nowTime) / 1000; // times是剩余时间的总的毫秒数
var h = parseInt(times / 60 / 60 % 24);
h = h < 10 ? '0' + h : h;//判断数值小于10的情况 如 0-9改为 00-09
hour.innerHTML = h;//更改div里面的内容 把h给获取元素hour的内容
var m = parseInt(times / 60 % 60);
m = m < 10 ? "0" + m : m;
minute.innerHTML = m;//同上
var s = parseInt(times % 60);
s = s < 10 ? "0" + s : s;
second.innerHTML = s;//同上
}
</script>
</html>
天数(天,小时,分钟,秒) 的倒计时方法原理也是相同的,
只需要在上述代码中加入天数,以2021考研倒计时为例,具体代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
#box {
width: 300px;
height: 300px;
margin: 200px 200px;
background: red;
position: relative;
}
.txt {
width: 150px;
height: 50px;
text-align: center;
line-height: 50px;
color: #fff;
font-size: 20px;
font-weight: 900;
position: absolute;
left: 55px;
top: 50px;
}
.day {
left: 20px;
}
.d_h {
left: 68px;
}
/* .hour {
left: 20px;
} */
.hour {
left: 80px;
}
.h_m {
left: 128px;
}
.minute {
left: 140px;
}
.m_s {
right: 105px;
}
.second {
left: 200px;
}
.day,
.hour,
.minute,
.second {
position: absolute;
top: 200px;
color: #fff;
font-weight: 800;
font-size: 20px;
text-align: center;
line-height: 40px;
width: 40px;
height: 40px;
background: black;
}
.d_h,
.h_m,
.m_s {
color: #fff;
font-size: 20px;
font-weight: 900;
position: absolute;
bottom: 70px;
}
.words {
margin-top: 150px;
font-size: 18px;
color: yellowgreen;
margin-left: 50px;
text-align: center;
position: absolute;
}
</style>
</head>
<body>
<div id="box">
<div class="txt">2021考研倒计时</div>
<div class="words">第一场 <strong>08:30</strong> 倒计时</div>
<div class="day"></div>
<span class="d_h">:</span>
<div class="hour"></div>
<!-- 小时与分钟之间的冒号 -->
<span class="h_m">:</span>
<div class="minute"></div>
<!-- 分钟与秒之间的冒号 -->
<span class="m_s">:</span>
<div class="second"></div>
</div>
</body>
<script>
//1、获取元素
var day = document.querySelector(".day");
var hour = document.querySelector(".hour");
var minute = document.querySelector(".minute");
var second = document.querySelector(".second");
var inputTime = +new Date("2020-12-25 08:30:00");//倒计时的结束时间,自己设置时间
countDown();//先调用一次这个函数 防止第一次刷新页面有空白
//2、开启定时器
setInterval(countDown, 1000);//1000毫秒,每一秒钟调用一次函数
//3、倒计时-时分秒函数
function countDown() {
var nowTime = +new Date(); //返回的是当前时间的总的毫秒数
var times = (inputTime - nowTime) / 1000; // times是剩余时间的总的毫秒数
var d = parseInt(times / 60 / 60 / 24);
d = d < 10 ? '0' + d : d;
day.innerHTML = d;
var h = parseInt(times / 60 / 60 % 24);
h = h < 10 ? '0' + h : h;//判断数值小于10的情况 如 0-9改为 00-09
hour.innerHTML = h;//更改div里面的内容 把h给获取元素hour的内容
var m = parseInt(times / 60 % 60);
m = m < 10 ? "0" + m : m;
minute.innerHTML = m;//同上
var s = parseInt(times % 60);
s = s < 10 ? "0" + s : s;
second.innerHTML = s;//同上
}
</script>
</html>
好啦,以上就是今天的分享,喜欢就点个赞吧~~