首页 > Web开发 > 详细

JS超酷时钟的制作

时间:2019-06-27 13:15:52      阅读:108      评论:0      收藏:0      [点我收藏+]

技术分享图片

通过补充代码,实现时钟实时显示当前时间:年、月、日、时、分、秒、日期。

<!DOCTYPE html>
<html>

<head lang="zh-CN">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
    <title>超酷时钟的制作</title>
    <style>
        .container {
            text-align: center;
            font-size: 28px;
            color: #ccc;
            background-color: #ccc;
            text-shadow: 1px 1px 1px #000, -1px -1px 1px #fff;
        }
        .container span{
            text-shadow: -1px -1px 1px #000, 1px 1px 1px #fff;
        }
    </style>
</head>

<body>
    <div class="container">
        <p>超酷时钟</p>
        <p><span class=‘year‘>2012</span><span class=‘month‘>09</span><span class=‘date‘>02</span></p>
        <p><span class=‘hours‘>08</span><span class=‘minutes‘>08</span><span class=‘seconds‘>08</span>星期<span
                class=‘day‘></span></p>
    </div>
    <script type="text/javascript">
  //补充代码
    </script>
</body>

</html>

参考答案:

<!DOCTYPE html>
<html>

<head lang="zh-CN">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
    <title>数码时钟</title>
    <style>
        .container {
            text-align: center;
            font-size: 28px;
            color: #ccc;
            background-color: #ccc;
            text-shadow: 1px 1px 1px #000, -1px -1px 1px #fff;
        }

        .container span {
            text-shadow: -1px -1px 1px #000, 1px 1px 1px #fff;
        }
    </style>
</head>

<body>
    <div class="container">
        <p>超酷时钟</p>
        <p><span class=‘year‘>2012</span><span class=‘month‘>09</span><span class=‘date‘>02</span></p>
        <p><span class=‘hours‘>08</span><span class=‘minutes‘>08</span><span class=‘seconds‘>08</span>星期<span
                class=‘day‘></span></p>
    </div>
    <script type="text/javascript">
        window.onload = function () {
            function byClass(arg) {
                return typeof (arg) === string ? document.getElementsByClassName(arg)[0] : arg;
            }
            var oYear = byClass(year);
            var oMonth = byClass(month);
            var oDate = byClass(date);
            var oHour = byClass(hours);
            var oMinute = byClass(minutes);
            var oSecond = byClass(seconds);
            var oDay = byClass(day);
            function toDuble(n) {
                if (n < 10) {
                    return 0 + n;
                } else {
                    return n;
                }

            }

            function getTime(arg) {
                var time = null;
                var oDate = new Date();
                switch (arg) {
                    case year:
                        time = oDate.getFullYear();
                        break;
                    case month:
                        time = toDuble(oDate.getMonth() + 1);
                        break;
                    case date:
                        time = toDuble(oDate.getDate());
                        break;
                    case hours:
                        time = toDuble(oDate.getHours());
                        break;
                    case minutes:
                        time = toDuble(oDate.getMinutes());
                        break;
                    case seconds:
                        time = toDuble(oDate.getSeconds());
                        break;
                    case day:
                        time = daySwith(oDate.getDay());
                        break;
                }
                return time;
            }

            function daySwith(arg) {
                var day = null;
                switch (arg) {
                    case 0:
                        day = ;
                        break;
                    case 1:
                        day = ;
                        break;
                    case 2:
                        day = ;
                        break;
                    case 3:
                        day = ;
                        break;
                    case 4:
                        day = ;
                        break;
                    case 5:
                        day = ;
                        break;
                    case 6:
                        day = ;
                        break;
                    default:
                        day = "出现bug";
                }
                return day;
            }
            function switchContent() {
                oYear.innerHTML = getTime(year);
                oMonth.innerHTML = getTime(month);
                oDate.innerHTML = getTime(date);
                oHour.innerHTML = getTime(hours);
                oMinute.innerHTML = getTime(minutes);
                oSecond.innerHTML = getTime(seconds);
                oDay.innerHTML = getTime(day);
            }
            setInterval(switchContent, 1000);
            switchContent();
        }
    </script>
</body>

</html>

 

JS超酷时钟的制作

原文:https://www.cnblogs.com/f6056/p/11096349.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!