时钟之CSS+JS版

写在前面

此版本绘制的时钟基于CSS+JS模式。

优点操作简单,缺点当然是不够灵活。下一篇会基于HTML5的canvas标签,使用JS绘制。会更灵活,元素更加丰富。

HTML代码

html 复制代码
<div class="box">
    <article class="clock">
        <!--  每个指针都需要一个 *-container容器 -->
        <div class="hours-container">
            <div class="hours"></div>
        </div>
        <div class="minutes-container">
            <div class="minutes"></div>
        </div>
        <div class="seconds-container">
            <div class="seconds"></div>
        </div>
    </article>
</div>

CSS代码

css 复制代码
.box {
    width: 10rem;
    height: 10rem;
    background: rgb(205,205,205, .1);
    border-radius: 1rem;
    margin: 5% auto;
    display: flex;
    justify-content: center;
    align-items: center;
}
/* .box使用 Flex 布局方式,并且使其中的 .clock水中、垂直方向都居中。*/
.clock {
    width: 10rem;
    height: 10rem;
    background: rgb(244, 244, 244, .1) url(../img/clock.png) no-repeat center;
    background-size: cover;
    background-size: 100%;
    border-radius: 50%;
    position: relative;
}
/*添加中心轴:使用 CSS3 中的 伪元素 为时钟添加实心小圆点,指针都围着这个点转。*/
.clock:after {
    content: "";  /* 这句 content: ''; 是必须的,不然这个伪元素不会显示,即使指定了宽度和高度。 */
    width: 1rem;
    height: 1rem;
    background: #000;
    border-radius: 50%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);   /* !!!向左上移动自身的50% */
    z-index: 10;  /* 是为了使这个小圆点在视图的最上层,遮挡住指针交叉的地方 */
}
/*由于相对定位是从元素的左上角开始计算的,所以 top: 50%; left: 50%; 不能使这个小圆点在 Clock 的中心,使用 transform: translate(-50%,-50%); 向左上方移动自身宽度或高度的 50%*/

/*指针容器: 容器被放置在 Clock 的上方*/
.hours-container,.minutes-container,.seconds-container {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
}
/*添加指针:设置指针样式*/
.hours {
    width: 3%;
    height: 20%;
    background: rgb(0, 0, 0, .8);
    transform-origin: 50% 100%; /* transform-origin: 50% 90%; 规定指针旋转的位置为:X 方向的中心线 和 Y 方向的 90% 处这条线的交叉点。*/
    position: absolute;
    top: 35%;
    left: 48.5%;
}
.minutes {
    width: 2%;
    height: 30%;
    background: rgb(13, 2, 223, .8);
    transform-origin: 50% 100%; 
    position: absolute;
    top: 24%;
    left: 49%;

}
.seconds {
    width: 1%;
    height: 40%;
    background: rgb(255, 0, 0, .8);
    transform-origin: 50% 100%;
    position: absolute;
    top: 20%;
    left: 49.5%;
}

@keyframes rotate {
  100% {
    transform: rotateZ(360deg);
  }
}

JS代码

javascript 复制代码
function frame() {
    const now = new Date();
    const hours = now.getHours();
    const minutes = now.getMinutes();
    const seconds = now.getSeconds();
    const sDeg = (seconds % 60) * 6;// 描述实际对应度数
    const mDeg = (minutes % 60) * 6 + (seconds % 60) * 6 / 360 * 6;// 分针实际对应度数 + 秒针跑过折算度数
    const hDeg = (hours > 12 ? hours % 24 : hours % 12) * 30 + (minutes % 60) * 6 / 360 * 30;// 时针实际对应度数 + 分针跑过折算度数

    document.querySelector('.seconds-container').style.transform = "rotate(" + sDeg + "deg)";
    document.querySelector('.minutes-container').style.transform = "rotate(" + mDeg + "deg)";
    document.querySelector('.hours-container').style.transform = "rotate(" + hDeg + "deg)";
}
window.onload = function() {
    frame();
    setInterval(frame, 1000);
}

实现效果

相关推荐
zhangxingchao27 分钟前
AI大模型核心七:从 Workflow、RAG、记忆治理到幂等性
前端·人工智能·后端
gyx_这个杀手不太冷静1 小时前
Agent开发进阶指南(第 1 章):AI Agent 到底是什么?
前端·agent·ai编程
小小小小宇2 小时前
模型相关知识
前端
小小小小宇2 小时前
模型相关知识二
前端
IT_陈寒2 小时前
Python装饰器竟然偷偷改了函数名?这个坑我爬了三天
前端·人工智能·后端
free352 小时前
作用域链与 Environment:变量查找、遮蔽和赋值怎么实现
javascript
Momo__3 小时前
Vite 8.1 打包开发模式——10000 组件冷启动 15 倍,"No Bundle Dev" 七年后被自己终结
前端·vue.js·vite
YHL3 小时前
🤖 Agent 智能体开发实战 · 第一课:Tool Use —— 让大模型自动干活
前端·javascript·人工智能
Monki3 小时前
AI 规范式 UI 设计,一键批量出页,高效落地不翻车
前端
山河木马3 小时前
渲染管线-计算得到gl_FragColor(片元着色器)之后续GPU流程
前端·webgl·计算机图形学