画一个时钟(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>
相关推荐
Violet5157 分钟前
ECMAScript规范解读——this的判定
javascript
知识分享小能手39 分钟前
Html5学习教程,从入门到精通,HTML5 简介语法知识点及案例代码(1)
开发语言·前端·javascript·学习·前端框架·html·html5
IT、木易42 分钟前
大白话React第二章深入理解阶段
前端·javascript·react.js
晚安7201 小时前
Ajax相关
前端·javascript·ajax
bin91531 小时前
DeepSeek 助力 Vue 开发:打造丝滑的单选按钮(Radio Button)
前端·javascript·vue.js·ecmascript·deepseek
qianmoQ1 小时前
第五章:工程化实践 - 第五节 - Tailwind CSS 常见问题解决方案
前端·css
那就可爱多一点点1 小时前
超高清大图渲染性能优化实战:从页面卡死到流畅加载
前端·javascript·性能优化
不能只会打代码2 小时前
六十天前端强化训练之第一天HTML5语义化标签深度解析与博客搭建实战
前端·html·html5
eggcode4 小时前
CSS通过webkit-scrollbar设置滚动条样式
css·webkit·scrollbar
十步杀一人_千里不留行4 小时前
React Native 下拉选择组件首次点击失效问题的深入分析与解决
javascript·react native·react.js