2020 新年倒计时

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-26 09:20:46

效果显示

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        h1 {
            margin: 200px auto;
            text-align: center;
        }
        
        span {
            color: purple;
        }
    </style>
</head>

<body>
    <h1 style="color:pink">
        2020新年倒计时: <span></span><span></span> 小时 <span></span><span></span></h1>
    <script>
        var goalDate = new Date(2020, 0, 24);

        var span = document.getElementsByTagName('span');

        // 将单数改为双数(6 ~ 06)
        function toTwo(params) {
            // console.log(params);
            if (params < 10) {
                return '0' + params
                    // console.log(params);
            } else {
                return params
            }
        }

        function gapTime() {
            var localTime = new Date();
            gap_time = goalDate - localTime

            gap_day = parseInt(gap_time / 1000 / 60 / 60 / 24);
            gap_day = toTwo(gap_day);
            gap_hour = parseInt(gap_time / 1000 / 60 / 60 % 24);
            gap_hour = toTwo(gap_hour);
            gap_minute = parseInt(gap_time / 1000 / 60 % 60);
            gap_minute = toTwo(gap_minute);
            gap_seconds = parseInt(gap_time / 1000 % 60);
            gap_seconds = toTwo(gap_seconds);

            span[0].innerHTML = gap_day;
            span[1].innerHTML = gap_hour;
            span[2].innerHTML = gap_minute;
            span[3].innerHTML = gap_seconds;
        }
        gapTime();
        setInterval(gapTime, 1000);
    </script>
</body>

</html>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!