简单实现2020年倒计时

Posted ahuntsun-blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单实现2020年倒计时相关的知识,希望对你有一定的参考价值。

运用简单的css、htmljavascript代码实现2020年倒计时。


html:


<!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>
    <link rel="stylesheet" href="index.css">
</head>

<body>
    <div class="container">
        <h2><span>Countdown to New Year</span>2020</h2>
        <div class="countdown">
            <div id="day">NA</div>
            <div id="hour">NA</div>
            <div id="minute">NA</div>
            <div id="second">NA</div>
        </div>
    </div>
    <script>
        var countDate = new Date('JAN 1,2020 00:00:00').getTime();

        function newYear() {
            let now = new Date().getTime();
            let gap = countDate - now;

            let second = 1000;
            let minute = second * 60;
            let hour = minute * 60;
            let day = hour * 24;

            let d = Math.floor(gap / (day));
            let h = Math.floor((gap % (day)) / (hour));
            let m = Math.floor((gap % (hour)) / (minute));
            let s = Math.floor((gap % (minute)) / (second));

            document.getElementById('day').innerText = d;
            document.getElementById('hour').innerText = h;
            document.getElementById('minute').innerText = m;
            document.getElementById('second').innerText = s;
        }
        setInterval(() => {
            newYear();
        }, 1000);
    </script>
</body>

</html>

css样式:


@import url('https://fonts.googleapis.com/css?family=Poppins:100,200,300,400,500,600,700,800,900');
* {
    margin: 0;
    padding: 0;
    font-family: 'Poppins', sans-serif;
}

body {
    /* background: rgba(0, 0, 0, 0.7) url(../images/2.png); */
    background: rgba(0, 0, 0, 0.5) url(../images/3.png);
    background-attachment: fixed;
    background-blend-mode: hard-light;
}

.container {
    position: absolute;
    top: 80px;
    left: 100px;
    right: 100px;
    bottom: 80px;
    /* background: url(../images/2.png); */
    background: url(../images/3.png);
    background-attachment: fixed;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    box-shadow: 0 50px 50px rgba(0, 0, 0, 0.5), 0 0 0 100px rgba(0, 0, 0, 0.1);
}

.container h2 {
    text-align: center;
    font-size: 10em;
    line-height: 0.7em;
    color: #333;
    margin-top: -80px;
}

.container h2 span {
    display: block;
    font-weight: 300;
    letter-spacing: 6px;
    font-size: 0.2em;
}

.countdown {
    display: flex;
    margin-top: 50px;
}

.countdown div {
    position: relative;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
    background: #333;
    color: #fff;
    margin: 0 15px;
    font-size: 3em;
    font-weight: 500;
}

.countdown div::before {
    content: '';
    position: absolute;
    bottom: -30px;
    left: 0;
    width: 100%;
    height: 35px;
    background: #ff0;
    color: #333;
    font-size: 0.35em;
    line-height: 35px;
    font-weight: 300;
}

.countdown div#day::before {
    content: 'Days';
}

.countdown div#hour::before {
    content: 'Hours';
}

.countdown div#minute::before {
    content: 'Minutes';
}

.countdown div#second::before {
    content: 'Seconds';
}

效果展示


技术图片

学习自YouTube@Online Tutorials

以上是关于简单实现2020年倒计时的主要内容,如果未能解决你的问题,请参考以下文章

js倒计时的实现

#yyds干货盘点#愚公系列2023年02月 .NET/C#知识点-程序运行计时的总结

python使用上下文对代码片段进行计时,非装饰器

70行JavaScript代码实现除夕夜倒计时 零基础也能学会~

高分求一段 PHP 实时显示倒计时 代码

Microsoft Word使用域代码(field code)实现日期倒计时,类似距离xx年xx月xx日还有xx天