仿京东秒杀倒计时

目标:
使用加速器实现倒计时效果,并将剩余时间显示
代码里面另外引用了iconfont矢量图标库,感兴趣的可以看一下官网的案例,我的代码包链接 提取码:7pas

<link rel="stylesheet" href="iconfont/iconfont.css">

HTML5部分

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="iconfont/iconfont.css">
    <link rel="stylesheet" href="css/jd.css">
    <title>京东秒杀</title>
</head>
<body>
    <div class="JDBox">
        <p class="secKill">京东秒杀</p>
        <p class="en">FLASH DEALS</p>
        <p class="iconfont icon-shandian"></p>
        <p class="spare">本场距离结束还剩</p>
        <div class="time">
            <p class="hour" id="hour"></p>
            <p class="min" id="min"></p>
            <p class="sec" id="sec"></p>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
        </div>
    </div>
    <script src="js/jd.js"></script>
</body>
</html>

CSS部分

*{
    margin: 0;
    padding: 0;
    list-style: none;
}

.JDBox>p{
    text-align: center;
}

.JDBox{
    margin-top: 40px;
    margin-left: 60px;

    display: flex;
    flex-direction: column;
    align-content: center;
    width: 230px;
    height: 340px;
    background-color: rgb(234,54,50);
}

.JDBox>.secKill{
    margin-top: 50px;
    margin-bottom: 5px;
    color: white;
    font-size: 40px;

}

.JDBox>.en{
    color: rgb(245,167,165);
    font-size: 23px;
}

.JDBox>p:nth-child(3){
    margin-top: 8px;
    margin-bottom: 16px;
    color: white;
    font-size: 46px;
}

.JDBox>.spare{
    margin-bottom: 25px;
    color: white;
    font-size: 19px;
}

.JDBox>.time{
    position: relative;
    margin: 0 auto;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-content: space-between;
    flex-wrap: wrap;
    width: 157px;
    height: 52px;
    /*background-color: pink;*/
}
.JDBox>.time>div{
    width: 48px;
    height: 25px;
    background-color: rgb(47,52,58);
}

.JDBox>.time>p{
    position: absolute;
    width: 38px;
    height: 38px;
    /*background-color: #0a6ebd;*/
    line-height: 38px;
    text-align: center;
    font-size: 25px;
    color: white;
}

.JDBox>.time>.hour{
    top: 7px;
    left: 5px;
}

.JDBox>.time>.min{
    top: 7px;
    left: 59px;
}

.JDBox>.time>.sec{
    top: 7px;
    left: 114px;
}

JavaScript部分

/**
 * 计算时间差,获取所剩 【小时,分钟,秒】
 * @return {(number|number)[]}
 */
const getTime =()=> {
    let nowTime = new Date(),
        endTime = new Date("2023-04-04 17:30:00"),
        leftTime = endTime - nowTime,
        hour = Math.floor(leftTime / (1000 * 60 * 60)),  //计算小时
        min = Math.floor(leftTime / (1000 * 60) % 60),  //计算分钟
        sec = Math.floor((leftTime / 1000) % 60);  //计算秒
    if(leftTime>=0){
        return [hour,min,sec];
    }else{
        return [];
    }
}

const showTime = (handle=null) => {
    document.querySelector('#sec').innerText
        = getTime().toString();
    if(getTime().length>0){
        hour.innerText = getTime()[0].toString();
        min.innerText = getTime()[1].toString();
        sec.innerText = getTime()[2].toString();
    }else{
        hour.innerText = "0";
        min.innerText = "0";
        sec.innerText = "0";
        clearInterval(handle);
    }
}

let hour = document.querySelector('#hour');
let min = document.querySelector('#min');
let sec = document.querySelector('#sec');

setInterval(()=>{
    showTime(this);
},1000);

document.addEventListener("DOMContentLoaded",()=>{
    showTime();
});

效果图:

仿京东秒杀倒计时

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值