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

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

效果:

代码: 

<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>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一份使用 HTML+CSS+JS 实现炫酷时钟的示例代码: ```html <!DOCTYPE html> <html> <head> <title>Cool Clock</title> <style type="text/css"> body { background-color: #333; } .clock { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); border-radius: 50%; background-color: #fff; box-shadow: 0 0 20px rgba(0, 0, 0, 0.1); width: 300px; height: 300px; display: flex; align-items: center; justify-content: center; flex-direction: column; } .clock .hour, .clock .minute, .clock .second { position: relative; width: 70%; height: 70%; margin: auto; border-radius: 50%; background-color: #333; display: flex; align-items: center; justify-content: center; font-size: 2rem; color: #fff; } .clock .hour:before, .clock .minute:before, .clock .second:before { content: ""; position: absolute; top: 0; left: 50%; transform: translate(-50%, 0); height: 50%; width: 5px; background-color: #fff; border-radius: 5px; } .clock .hour { z-index: 3; } .clock .hour:before { height: 30%; } .clock .minute { z-index: 2; } .clock .minute:before { height: 40%; } .clock .second { z-index: 1; } .clock .second:before { height: 50%; } .clock .tick { position: absolute; top: 0; left: 50%; transform: translate(-50%, 0); height: 10%; width: 5px; background-color: #fff; border-radius: 5px; transform-origin: center 90%; animation: tick 1s linear infinite; } .clock .tick:nth-child(5n+1) { height: 20%; } .clock .tick:nth-child(10n+1) { height: 30%; } .clock .tick:nth-child(15n+1) { height: 40%; } .clock .tick:nth-child(20n+1) { height: 50%; } @keyframes tick { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } </style> </head> <body> <div class="clock"> <div class="hour"></div> <div class="minute"></div> <div class="second"></div> <div class="tick"></div> <div class="tick"></div> <div class="tick"></div> <div class="tick"></div> <div class="tick"></div> <div class="tick"></div> <div class="tick"></div> <div class="tick"></div> <div class="tick"></div> <div class="tick"></div> </div> <script type="text/javascript"> function updateClock() { var now = new Date(); var hour = now.getHours(); var minute = now.getMinutes(); var second = now.getSeconds(); var hourHand = document.querySelector(".hour"); var minuteHand = document.querySelector(".minute"); var secondHand = document.querySelector(".second"); var hourAngle = (hour % 12) / 12 * 360 + minute / 60 * 30; var minuteAngle = minute / 60 * 360; var secondAngle = second / 60 * 360; hourHand.style.transform = "rotate(" + hourAngle + "deg)"; minuteHand.style.transform = "rotate(" + minuteAngle + "deg)"; secondHand.style.transform = "rotate(" + secondAngle + "deg)"; } setInterval(updateClock, 1000); </script> </body> </html> ``` 这份代码实现了一个圆形的时钟时钟的样式可以通过 CSS 进行自定义。时钟的指针是通过 CSS 的 `transform: rotate()` 属性来控制旋转角度的,指针的角度是根据当前时间计算出来的。时钟上的刻度线是通过 CSS 的 `animation` 属性来实现的,用 `transform-origin` 控制旋转中心,用 `@keyframes` 定义动

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值