原生js实现倒计时功能,创建元素div插入元素,时间结束替换倒计时功能,移除元素

26 篇文章 0 订阅
21 篇文章 0 订阅
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
<style type="text/css" rel="stylesheet">
.timer {width: 340px;height: 75px;position: absolute;display: flex;justify-content: space-between;
    left: 50%;top: 62%;transform: translateX(-50%);}
.hours{width: 45px;height: 75px; border-radius: 10px;font-size: 45px;color: #000;
    background-color: #666;text-align: center;line-height: 75px;font-weight: 700}
.newCreatedDiv {width: 100%;height: 100%;background-color:#444;color: white;font-size: 24px;
    font-weight:700;text-align: center;line-height: 75px;text-decoration: none}
</style>
<body>
<div class="timer" id="timerStart">
        <div id="hoursTen" class="hours hoursTen"></div>
        <div id="hoursSingle" class="hours hoursSingle"></div>
        <div class="point">
            <span></span><span></span>
        </div>
        <div id="minutesTen" class="hours minutesTen"></div>
        <div id="minutesSingle" class="hours minutesSingle"></div>
        <div class="point">
            <span></span><span></span>
        </div>
        <div id="secondTen" class="hours secondTen"></div>
        <div id="secondSingle" class="hours secondSingle"></div>
</div>
<script type="text/javascript">
    function checkTime(i) {
        if (i < 10) {
            i = "0" + i;
        }
        return i;
    }


    function GetTime() {
        var EndTime = new Date('2018/01/20 00:00:00');
        var NowTime = new Date();
        var activityTime = EndTime.getTime() - NowTime.getTime();
        var d = 0;
        var h = 0;
        var m = 0;
        var s = 0;
        if (activityTime >= 0) {
            d = Math.floor(activityTime / 1000 / 60 / 60 / 24);
            h = Math.floor(activityTime / 1000 / 60 / 60 % 24);
            m = Math.floor(activityTime / 1000 / 60 % 60);
            s = Math.floor(activityTime / 1000 % 60);
            var day = checkTime(d);
            var hours = checkTime(h);
            var minutes = checkTime(m);
            var seconds = checkTime(s);
            //将4个时间点存入数组,单独取出来转换成字符串,切割开,再转回成数字类型,分别放入对应的元素内,显示出来
            var arr = [];
            arr.push(day, hours, minutes, seconds);
            var arrdd = arr[0].toString().split("");
            var arrhh = arr[1].toString().split("");
            var arrmm = arr[2].toString().split("");
            var arrss = arr[3].toString().split("");
            var hoursTen = parseInt(arrhh[0]);
            var hoursSingle = parseInt(arrhh[1]);
            var minutesTen = parseInt(arrmm[0]);
            var minutesSingle = parseInt(arrmm[1]);
            var secondTen = parseInt(arrss[0]);
            var secondSingle = parseInt(arrss[1]);
            document.getElementById("hoursTen").innerHTML = hoursTen;
            document.getElementById("hoursSingle").innerHTML = hoursSingle;
            document.getElementById("minutesTen").innerHTML = minutesTen;
            document.getElementById("minutesSingle").innerHTML = minutesSingle;
            document.getElementById("secondTen").innerHTML = secondTen;
            document.getElementById("secondSingle").innerHTML = secondSingle;
        } else {
            var timerOver = document.getElementById("timerStart");
            var childs = timerOver.childNodes;
            for (var i = childs.length - 1; i >= 0; i--) {
                timerOver.removeChild(childs[i]);
            }
            var btn = document.createElement('a');
            var text = document.createTextNode('立即抢购!');
            btn.appendChild(text);
            btn.className = 'newCreatedDiv';
            btn.href = "https://www.baidu.com/";
            timerOver.appendChild(btn);
            clearInterval(clearTimerOver)
        }
    }


    var clearTimerOver = setInterval(GetTime, 1000);
</script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值