image.png
<!-- 倒计时 -->
<div class="count_down">
还剩
<em>0</em>
<em>2</em>
天
<em>1</em>
<em>5</em>
时
<em>0</em>
<em>0</em>
分
</div>
// css
.header .count_down {
font-size: 36/@rem;
color: #999999;
line-height: 60/@rem;
display: flex;
justify-content: center;
align-items: center;
margin-top: 15/@rem;
}
.header .count_down em {
display: block;
width: 52/@rem;
height: 60/@rem;
background-color: #00c0ff;
border-radius: 2/@rem;
font-style: normal;
font-size: 56/@rem;
color: #FFFFFF;
text-align: center;
line-height: 60/@rem;
margin: 0 6/@rem;
}
countDown();
function addZero(i) {
return i < 10 ? "0" + i: i + "";
}
function countDown() {
// 当前时间戳
var nowtime = new Date();
// 结束时间戳
var endtime = new Date("2021/04/24,16:00:00");
// 剩余时间(秒) [除以1000是因为不需要毫秒]
var lefttime = parseInt((endtime.getTime() - nowtime.getTime()) / 1000);
// 返回剩余d天
var d = parseInt(lefttime / (24*60*60));
// 返回剩余h小时
var h = parseInt(lefttime / (60 * 60) % 24);
// 返回剩余m分
var m = parseInt(lefttime / 60 % 60);
// 返回剩余s秒
// var s = parseInt(lefttime % 60);
// ad 为 十位数字
var ad=0;
// bd 为 个位数字
var bd=0;
var ah = 0;
var bh = 0;
var am = 0;
var bm = 0;
// var as = 0;
// var bs = 0;
//若是个位数字 则在前面+0下同 例如:4 = 04
d = addZero(d);
// 分割是两位时间戳,下同
ad=d.slice(d.length-2,d.length-1);
bd=d.slice(d.length-1,d.length);
h = addZero(h);
ah=h.slice(h.length-2,h.length-1);
bh=h.slice(h.length-1,h.length);
m = addZero(m);
am=m.slice(m.length-2,m.length-1);
bm=m.slice(m.length-1,m.length);
// s = addZero(s);
// as=s.slice(s.length-2,s.length-1);
// bs=s.slice(s.length-1,s.length);
// 倒计时标签内没有子元素时
// document.querySelector(".count_down").innerHTML = `活动倒计时 ${d} 01 天 ${h} 时 ${m} 分 ${s} 秒`;
// 倒计时标签内有子元素时
document.querySelector(".count_down").innerHTML = `还剩<em>${ad}</em><em>${bd}</em>天<em>${ah}</em><em>${bh}</em>时<em>${am}</em><em>${bm}</em>分`;
// 活动已结束
if (lefttime <= 0) {
document.querySelector(".count_down").innerHTML = "活动已结束";
return;
}
setTimeout(countDown, 1000);
}
}
// 倒计时
function getRTime(endTime){
var BargainTimer = setInterval(function () {
var NowTime = new Date();
var t = endTime * 1000 - NowTime.getTime();
if(t>0){
var d=Math.floor(t/1000/60/60/24);
var h=Math.floor(t/1000/60/60%24);
var m=Math.floor(t/1000/60%60);
var s=Math.floor(t/1000%60);
if(d<10) d='0'+d;
if(h<10) h='0'+h;
if(m<10) m='0'+m;
if(s<10) s='0'+s;
document.getElementById("t_d").innerHTML = d ;
document.getElementById("t_h").innerHTML = h ;
document.getElementById("t_m").innerHTML = m ;
document.getElementById("t_s").innerHTML = s ;
}else{
clearInterval(BargainTimer);
}
},1000);
}
网友评论