简单实现2020年倒计时

运用简单的css、html和JavaScript代码实现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

猜你喜欢

转载自www.cnblogs.com/AhuntSun-blog/p/12079239.html