码迷,mamicode.com
首页 > 其他好文 > 详细

简单实现2020年倒计时

时间:2019-12-22 12:57:56      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:abs   mod   img   doc   before   play   shadow   https   let   

运用简单的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

简单实现2020年倒计时

标签:abs   mod   img   doc   before   play   shadow   https   let   

原文地址:https://www.cnblogs.com/AhuntSun-blog/p/12079239.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!