html css js 实现简易时钟

 基本思路:

        1. 通过日期对象获取当前时间

        2. 利用getHours, getMinutes, getSeconds获取当前的时分秒

        3. 分针的位置 = rotate(当前分钟*6 deg)

        4. 秒针的位置 = rotate(当前秒数*6 deg)

        5. 时针的位置 = rotate(当前小时*30 deg)

        6. 利用setInterval每秒钟重新移动一次时分秒针的位置

以下是html和js代码 



    

以下是css代码

    div {
            position: relative;
            width: 100px;
            height: 100px;
            background: #fff;
            border-radius: 50%;
            border: 6px solid #000;
        }

        span {
            position: absolute;
            left: 50%;
            transform: translate(-50%);
            top: 10px;
            width: 3px;
            height: 40px;
            background-color: #000;
            transform-origin: bottom;
        }

        b {
            position: absolute;
            left: 50%;
            transform: translate(-50%);
            top: 15px;
            width: 4px;
            height: 35px;
            background-color: red;
            transform-origin: bottom;
        }

        i {
            position: absolute;
            left: 50%;
            transform: translate(-50%);
            top: 20px;
            width: 5px;
            height: 30px;
            background-color: blueviolet;
            transform-origin: bottom;
        }

        em {
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background-color: #000;
        }
  

以下是运行效果:

html css js 实现简易时钟_第1张图片

你可能感兴趣的:(小demo合集,css,javascript,html5)