You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:按教程实现的JS时钟指针无法旋转(附完整代码)

问题:JavaScript时钟指针无法旋转

我跟随教程学习编写JavaScript时钟,HTML与CSS效果和教程视频一致,但完成JavaScript代码编写后,时钟的指针无法旋转。以下是我的完整代码:

setInterval(setClock, 1000)

const hourHand = document.querySelector('[data-hour-hand]')
const minuteHand = document.querySelector('[data-minute-hand]')
const secondHand = document.querySelector('[data-second-hand]')


function setClock() {
    const currentDate = new Date()
    const secondsRatio = currentDate.getSeconds() / 60
    const minutesRatio = (secondsRatio + currentDate.getMinutes()) / 60
    const hoursRatio = (minutesRatio + currentDate.getHours()) / 12
    
}

setRotation(secondHand, secondsRatio)
setRotation(minuteHand, minutesRatio)
setRotation(hourHand, hoursRatio)


function setRotation(element, rotationRatio) {
    element.style.setProperty('--rotation', rotationRatio * 360)
}

setClock()
* {
    box-sizing: border-box;
    font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
}

body {
    background-color: aqua;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    overflow: hidden;
}

.clock {
    height: 500px;
    width: 500px;
    background-color: rgba(255, 255, 255, .8);
    border-radius: 50%;
    border: 2px solid black;
    position: relative;
}

.clock .number {
    --rotation: 0;
    position: absolute;
    width: 100%;
    height: 100%;
    text-align: center;
    transform: rotate(var(--rotation));
    font-size: 1.5rem;
}

.clock .number1 {--rotation: 30deg;}
.clock .number2 {--rotation: 60deg;}
.clock .number3 {--rotation: 90deg;}
.clock .number4 {--rotation: 120deg;}
.clock .number5 {--rotation: 150deg;}
.clock .number6 {--rotation: 180deg;}
.clock .number7 {--rotation: 210deg;}
.clock .number8 {--rotation: 240deg;}
.clock .number9 {--rotation: 270deg;}
.clock .number10 {--rotation: 300deg;}
.clock .number11 {--rotation: 330deg;}


.clock .hand {
    --rotation: 0;
    position: absolute;
    bottom: 50%;
    left: 50%;
    background-color: black;
    transform-origin: bottom;
    transform: translate(-50%) rotate(calc(var(--rotation) * 1deg));
    

}

.clock::after {

    content: '';
    position: absolute;
    background-color: black;
    z-index: 11;
    top: 50%;
    left: 50%;
    width: 15px;
    height: 15px;
    transform: translate(-50% , -50%);
    border-radius: 50%;

}

.clock .hand.second {
    width: 3px;
    height: 45%;
    background-color: red;
}

.clock .hand.minute {
    width: 7px;
    height: 40%;
    background-color: black;
}

.clock .hand.hour {
    width: 10px;
    height: 35%;
    background-color: black;
}
<div class="clock">
  <div class="hand hour" data-hour-hand></div>
  <div class="hand minute" data-minute-hand></div>
  <div class="hand second" data-second-hand></div>
  <div class="number number1">1</div>
  <div class="number number2">2</div>
  <div class="number number3">3</div>
  <div class="number number4">4</div>
  <div class="number number5">5</div>
  <div class="number number6">6</div>
  <div class="number number7">7</div>
  <div class="number number8">8</div>
  <div class="number number9">9</div>
  <div class="number number10">10</div>
  <div class="number number11">11</div>
  <div class="number number12">12</div>
</div>
解决方案

问题根源有两个:

  • 变量作用域问题:secondsRatio、minutesRatio、hoursRatio是setClock函数内部的局部变量,函数外部无法访问,所以你在函数外调用setRotation时这些变量未定义,自然无法更新指针角度。
  • 调用位置错误:更新指针旋转的setRotation语句必须放在setClock函数内部,这样每秒执行setClock时,都会重新计算当前时间对应的旋转比例,并立即更新指针。

修正后的JavaScript代码:

setInterval(setClock, 1000)

const hourHand = document.querySelector('[data-hour-hand]')
const minuteHand = document.querySelector('[data-minute-hand]')
const secondHand = document.querySelector('[data-second-hand]')

function setClock() {
    const currentDate = new Date()
    const secondsRatio = currentDate.getSeconds() / 60
    const minutesRatio = (secondsRatio + currentDate.getMinutes()) / 60
    const hoursRatio = (minutesRatio + currentDate.getHours()) / 12
    
    // 将setRotation调用移到函数内部,确保能访问局部变量
    setRotation(secondHand, secondsRatio)
    setRotation(minuteHand, minutesRatio)
    setRotation(hourHand, hoursRatio)
}

function setRotation(element, rotationRatio) {
    element.style.setProperty('--rotation', rotationRatio * 360)
}

setClock()

内容的提问来源于stack exchange,提问作者Rithvik Shervin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 11:45:32