画一个时钟(html+css+js)

这是一个很简约的时钟。。。。。。。

效果:

代码:

javascript 复制代码
<template>
  <div class="demo-box">
    <div class="clock">
      <ul class="mark">
        <li
            v-for="(rotate, index) in rotatedAngles"
            :key="index"
            :class="{ 'bold': index % 5 === 0 }"
            :style="{ transform: `translateY(90px) rotate(${rotate}deg)` }"
        ></li>
      </ul>
      <div id="min"></div>
      <div id="hour"></div>
      <div id="sec"></div>
    </div>
  </div>
</template>

<script setup>
import {onMounted, computed} from 'vue';

let timer = null

const rotatedAngles = computed(() => {
  return Array.from({length: 60}, (_, index) => index * 6);
})

const startTime = () => {
  const min = document.getElementById('min')
  const sec = document.getElementById('sec')
  const hour = document.getElementById('hour')

  const now = new Date()
  const s = now.getSeconds()
  const then = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0)
  const diffInMil = now.getTime() - then.getTime()
  const h = diffInMil / (1000 * 60 * 60)
  const m = h * 60;

  if (hour || min || sec) {
    hour.style.transform = `rotate(${h * 30}deg)`;
    min.style.transform = `rotate(${m * 6}deg)`;
    sec.style.transform = `rotate(${s * 6}deg)`;
  }
}

onMounted(() => {
  startTime()
  timer = setInterval(() => {
    setTimeout(startTime, 0)
  }, 1000)
})

</script>

<style scoped lang="scss">
.demo-box {
  width: 800px;
  margin: 100px auto;
}

ul li {
  list-style: none;
}

@mixin center {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
}

.clock {
  width: 180px;
  height: 180px;
  position: relative;
}

.mark {
  width: 180px;
  height: 180px;
  position: relative;
}

//画刻度尺
.mark li {
  position: absolute;
  width: 6px;
  height: 2px;
  background: #666;
  border-radius: 1px;
  transform-origin: 90px;
}

.mark li.bold {
  width: 12px;
  height: 4px;
  margin-top: -1px;
  background: #000;
  border-radius: 2px;
}

#sec {
  //中心圆点
  @include center;
  background: #303030;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  z-index: 3;

  &:before,
  &:after {
    display: block;
    content: "";
    position: absolute;
  }

  //秒针
  &:after {
    width: 2px;
    height: 4.4em;
    top: -4.3em;
    background: #303030;
    left: 0;
    right: 0;
    margin: 0 auto;
    border-radius: 1px;
  }
}

#min,
#hour {
  @include center;
  z-index: 2;
  background: #303030;
  transform-origin: bottom center;
}

//分针
#min {
  width: 4px;
  height: 4.2em;
  top: -4.2em;
  border-radius: 4px;
}

//时针
#hour {
  width: 4px;
  height: 3em;
  top: -3em;
  border-radius: 4px;
}
</style>
相关推荐
Eloudy2 小时前
ReAct 原理简介
前端·javascript·人工智能·react.js·agent·gpu
ZJU_统一阿萨姆3 小时前
【DSH】如何将 DeepSeek Harness 嵌入生产环境?万字解构 DeepSeek Agent Harness 的运行机制与安全边界
前端·javascript·安全
sunly_5 小时前
TypeScript总结:16、面向对象速查
前端·javascript·typescript
张元清5 小时前
React useInterval Hook:没有过期闭包的 setInterval (2026)
javascript·react.js
MXN_小南学前端5 小时前
React超长文本域中实现“返回顶部”浮动按钮
前端·javascript·react.js
gs801405 小时前
解构 Cordis:面向“时空可组合性”的 TypeScript 元框架深度剖析
前端·javascript·typescript
Highcharts.js6 小时前
Highcharts大数据渲染模块Boost实战与参数最佳实践表
javascript·数据可视化·boost·highcharts·大数据渲染·加速配置·参数表
岁岁种桃花儿6 小时前
Vue核心语法第一篇:Vue是什么?
前端·javascript·vue.js
Highcharts.js6 小时前
数据可视化避坑指南 ——开发中常见图表错误与修复方案
javascript·信息可视化·数据可视化·highcharts·数据可视化避坑·修复方案
观无7 小时前
若依EasyExcel实现单元格合并
开发语言·前端·javascript