js+css钟表

在这里插入图片描述

20230201-164607

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #watch .frame-face {
            position: relative;
            width: 30em;
            height: 30em;
            margin: 2em auto;
            border-radius: 15em;
            background: -webkit-linear-gradient(top, #f9f9f9, #666);
            background: -moz-linear-gradient(top, #f9f9f9, #666);
            background: linear-gradient(top, #f9f9f9, #666);
            box-shadow: 0.5em 0.5em 4em rgba(0, 0, 0, 0.8);
        }

        #watch .frame-face:before {
            content: '';
            width: 29.4em;
            height: 29.4em;
            border-radius: 14.7em;
            position: absolute;
            top: .3em;
            left: .3em;
            background: -webkit-radial-gradient(ellipse at center, rgba(246, 248, 249, 1) 0%, rgba(229, 235, 238, 1) 65%, rgba(205, 212, 217, 1) 66%, rgba(245, 247, 249, 1) 100%);
            background: -moz-radial-gradient(ellipse at center, rgba(246, 248, 249, 1) 0%, rgba(229, 235, 238, 1) 65%, rgba(205, 212, 217, 1) 66%, rgba(245, 247, 249, 1) 100%);
            background: radial-gradient(ellipse at center, rgba(246, 248, 249, 1) 0%, rgba(229, 235, 238, 1) 65%, rgba(205, 212, 217, 1) 66%, rgba(245, 247, 249, 1) 100%);
        }

        #watch .frame-face:after {
            content: '';
            width: 28em;
            height: 28em;
            border-radius: 14.2em;
            position: absolute;
            top: .9em;
            left: .9em;
            box-shadow: inset rgba(0, 0, 0, .2) .2em .2em 1em;
            border: .1em solid rgba(0, 0, 0, .2);
            background: -webkit-linear-gradient(top, #fff, #ccc);
            background: -moz-linear-gradient(top, #fff, #ccc);
            background: linear-gradient(top, #fff, #ccc);
        }

        #minute-marks li {
            z-index: 10;
            display: block;
            width: 0.2em;
            height: 0.6em;
            background: #929394;
            position: absolute;
            top: 50%;
            left: 50%;
            margin: -0.4em 0 0 -0.1em;
        }

        #digits {
            width: 30em;
            height: 30em;
            border-radius: 15em;
            position: absolute;
            top: 0;
            left: 50%;
            margin-left: -16em;
        }

        #digits li {
            font-size: 1.6em;
            display: block;
            width: 1.6em;
            height: 1.6em;
            position: absolute;
            top: 50%;
            left: 50%;
            line-height: 1.6em;
            text-align: center;
            margin: -.8em 0 0 -.8em;
            font-weight: bold;
            z-index: 11;
        }

        #digits li:nth-child(1) {
            transform: translate(7em, -0.5em)
        }

        #digits li:nth-child(2) {
            transform: translate(0, 6.5em)
        }

        #digits li:nth-child(3) {
            transform: translate(-7em, -0.5em)
        }

        #digits li:nth-child(4) {
            transform: translate(0, -7.5em)
        }

        #digits:before {
            content: '';
            width: 1.6em;
            height: 1.6em;
            border-radius: .8em;
            position: absolute;
            top: 50%;
            left: 50%;
            margin: -.8em 0 0 -.8em;
            background: #121314;
        }

        #digits:after {
            z-index: 10;
            content: '';
            width: 4em;
            height: 4em;
            border-radius: 2.2em;
            position: absolute;
            top: 50%;
            left: 50%;
            margin: -2.1em 0 0 -2.1em;
            border: .1em solid #c6c6c6;
            background: -webkit-radial-gradient(ellipse at center, rgba(200, 200, 200, 0), rgba(190, 190, 190, 1) 90%, rgba(130, 130, 130, 1) 100%);
            background: -moz-radial-gradient(ellipse at center, rgba(200, 200, 200, 0), rgba(190, 190, 190, 1) 90%, rgba(130, 130, 130, 1) 100%);
            background: radial-gradient(ellipse at center, rgba(200, 200, 200, 0), rgba(190, 190, 190, 1) 90%, rgba(130, 130, 130, 1) 100%);
        }

        #watch .hours-hand {
            width: .8em;
            height: 7em;
            border-radius: 0 0 .9em .9em;
            background: #232425;
            position: absolute;
            z-index: 10;
            bottom: 50%;
            left: 50%;
            margin: 0 0 -.8em -.4em;
            box-shadow: #232425 0 0 2px;
            transform-origin: 0.4em 6.2em;
            transform: rotate(-25deg);
        }

        #watch .hours-hand:before {
            content: '';
            background: inherit;
            width: 1.8em;
            height: .8em;
            border-radius: 0 0 .8em .8em;
            box-shadow: #232425 0 0 1px;
            position: absolute;
            top: -.7em;
            left: -.5em;
        }

        #watch .hours-hand:after {
            content: '';
            width: 0;
            height: 0;
            border: .9em solid #232425;
            border-width: 0 .9em 2.4em .9em;
            border-left-color: transparent;
            border-right-color: transparent;
            position: absolute;
            top: -3.1em;
            left: -.5em;
        }

        #watch .minutes-hand {
            z-index: 10;
            width: .8em;
            height: 12.5em;
            border-radius: .5em;
            background: #343536;
            position: absolute;
            bottom: 50%;
            left: 50%;
            margin: 0 0 -1.5em -.4em;
            box-shadow: #343536 0 0 2px;
            transform-origin: 0.4em 11em;
        }

        #watch .seconds-hand {
            z-index: 10;
            width: .2em;
            height: 14em;
            border-radius: .1em .1em 0 0/10em 10em 0 0;
            background: #c00;
            position: absolute;
            bottom: 50%;
            left: 50%;
            margin: 0 0 -2em -.1em;
            box-shadow: rgba(0, 0, 0, .8) 0 0 .2em;
            transform-origin: 0.1em 12em;
        }

        #watch .seconds-hand:after {
            content: '';
            width: 1.4em;
            height: 1.4em;
            border-radius: .7em;
            background: inherit;
            position: absolute;
            left: -.65em;
            bottom: 1.35em;
        }

        #watch .seconds-hand:before {
            content: '';
            width: .8em;
            height: 3em;
            border-radius: .2em .2em .4em .4em/.2em .2em 2em 2em;
            box-shadow: rgba(0, 0, 0, .8) 0 0 .2em;
            background: inherit;
            position: absolute;
            left: -.35em;
            bottom: -3em;
        }

        #watch .digital-wrap {
            width: 9em;
            height: 3em;
            border: .1em solid #222;
            border-radius: .2em;
            position: absolute;
            top: 50%;
            left: 50%;
            margin: 3em 0 0 -4.5em;
            overflow: hidden;
            background: #4c4c4c;
            background: -webkit-linear-gradient(top, #4c4c4c, #0f0f0f);
            background: -moz-linear-gradient(top, #4c4c4c, #0f0f0f);
            background: -ms-linear-gradient(top, #4c4c4c, #0f0f0f);
            background: -o-linear-gradient(top, #4c4c4c, #0f0f0f);
            background: linear-gradient(to bottom, #4c4c4c, #0f0f0f);
        }

        #watch .digital-wrap ul {
            float: left;
            width: 2.9em;
            height: 3em;
            border-right: .1em solid #000;
            color: #ddd;
            font-family: Consolas, monaco, monospace;
            text-align: center;
            line-height: 3em;
        }

        #watch .digital-wrap ul:last-child {
            width: 3em;
            border: none
        }
    </style>
</head>

<body>
    <div id="watch">
        <!-- 表盘 -->
        <div class="frame-face">
            <!-- 刻度 -->
            <ul id="minute-marks"></ul>
            <!-- 刻度的数字标识 -->
            <ul id="digits">
                <li>3</li>
                <li>6</li>
                <li>9</li>
                <li>12</li>
            </ul>
            <!-- 指针 -->
            <div class="hours-hand"></div>
            <div class="minutes-hand"></div>
            <div class="seconds-hand"></div>
            <!-- 数字表盘 -->
            <div class="digital-wrap">
                <ul class="digit-hours"></ul>
                <ul class="digit-minutes"></ul>
                <ul class="digit-seconds"></ul>
            </div>
        </div>
    </div>
    <script>
        window.onload = function () { // 生成刻度 
            let markWrap = document.getElementById('minute-marks')
            for (let index = 0; index < 60; index++) {
                let markItem = document.createElement('li')
                markItem.style.cssText = `transform:rotate(${index * 6}deg) translateY(-12.7em)`
                if (index % 5 == 0) {
                    markItem.style.width = "0.3em";
                    markItem.style.height = "1em";
                }
                markWrap.appendChild(markItem)
            }
        }
        let clockInstance = new Clock(document.querySelector('.frame-face'))
        function Clock(dom) {
            this.hour = 0
            this.minute = 0
            this.second = 0
            this.htmlDom = 0
            this.renderHTML = function() {
                const hourDom = dom.querySelector('.hours-hand')
                const minuteDom = dom.querySelector('.minutes-hand')
                const secondDom = dom.querySelector('.seconds-hand')
                console.log(this.second, '')
                if(this.second !== 0) {
                    !secondDom.style.transition ? secondDom.style.transition="all 0.1s linear": ''
                } else {
                    secondDom.style.transition=""
                }
                hourDom.style.transform = `rotate(${360 / 12 * this.hour}deg)`
                minuteDom.style.transform = `rotate(${360 / 60 * this.minute}deg)`
                secondDom.style.transform = `rotate(${360 / 60 * this.second}deg)`
            }
        }
        Clock.prototype.setTime = function (hour, minute, second) {
            this.hour =  hour% 12 || new Date().getHours() % 12
            this.minute = minute || new Date().getMinutes()
            this.second = second || new Date().getSeconds()
            this.renderHTML()
        }
        Clock.prototype.run = function() {
            let currentTimeFun = () => {
                this.second = new Date().getSeconds()
                this.minute = new Date().getMinutes() + parseInt(this.second / 60 * 100) / 100
                this.hour = new Date().getHours() + parseInt( this.minute / 60 * 100) / 100
                console.log(this.hour, this.minute)
                clockInstance.setTime(this.hour, this.minute)
                setTimeout(() => {
                    currentTimeFun()
                }, 1000)
            } 
            currentTimeFun()
        }
        clockInstance.setTime(18, 24, 38)
        clockInstance.run()
    </script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值