js实现数字时钟,按钮实现暂停开始

4 篇文章 0 订阅

用JS实现一个时钟,用一个按钮实现暂停开始时钟

<button id="clockBtn" onclick="StopStartClock()">暂停</button>

 

    //时钟
    //这个变量是清除计时方法的参数,必须是全局变量
    var myInterval=setInterval("TimeClock()",1000);
    function TimeClock() {
        var d=new Date();
        var t=d.toLocaleTimeString();
        document.getElementById("clock").innerHTML=t;
    }
    /*时钟停止开始,
    clearInterval(), 在创建计时方法时必须使用全局变量:
     */
    function StopStartClock() {
        var btnValue=document.getElementById("clockBtn").innerHTML;
        if(btnValue=="暂停"){
            clearInterval(myInterval);//清除计时方法
            document.getElementById("clockBtn").innerHTML="开始";
        }
        if(btnValue=="开始"){
            setInterval("TimeClock()",1000);//设置计时方法
            document.getElementById("clockBtn").innerHTML="暂停";
        }
    }
setInterval() - 间隔指定的毫秒数不停地执行指定的代码。第一个参数必需是function(){}形式,或者“函数名()”形式。
clearInterval(), 在创建计时方法时必须使用全局变量。

效果如下



 

 

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的钟表屏幕动画页面,使用了 HTML、CSS 和 JavaScript 技术实现: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Clock Screen Animation</title> <style> body { background-color: #000; display: flex; align-items: center; justify-content: center; height: 100vh; } .clock { position: relative; width: 300px; height: 300px; border-radius: 50%; background-color: #fff; box-shadow: 0 0 20px rgba(0, 0, 0, 0.5); } .hour, .minute, .second { position: absolute; width: 2px; height: 100px; background-color: #000; transform-origin: bottom center; border-radius: 4px 4px 0 0; } .hour { transform: rotate(180deg); } .minute { transform: rotate(90deg); } .second { background-color: #f00; transform: rotate(270deg); } .dot { position: absolute; width: 12px; height: 12px; background-color: #000; border-radius: 50%; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 1; } </style> </head> <body> <div class="clock"> <div class="hour"></div> <div class="minute"></div> <div class="second"></div> <div class="dot"></div> </div> <script> function updateClock() { const now = new Date(); const hour = now.getHours() % 12; const minute = now.getMinutes(); const second = now.getSeconds(); const hourHand = document.querySelector('.hour'); const minuteHand = document.querySelector('.minute'); const secondHand = document.querySelector('.second'); hourHand.style.transform = `rotate(${hour * 30 + minute / 2}deg)`; minuteHand.style.transform = `rotate(${minute * 6 + second / 10}deg)`; secondHand.style.transform = `rotate(${second * 6}deg)`; } setInterval(updateClock, 1000); </script> </body> </html> ``` 解析: 1. 在 HTML 中创建一个 `<div>` 容器,用于显示钟表屏幕。容器的样式设置为圆形,背景色为白色,边框设置为阴影效果。 2. 在容器中创建三个子元素,分别表示时针、分针和针。子元素的样式设置为细长的直线,高度为 100px,颜色为黑色。时针和分针的初始位置需要进行旋转,以使它们指向正确的刻度。针的样式和颜色不同,用红色表示。 3. 在容器中创建一个子元素,表示钟表的中心点。中心点的样式设置为圆形,颜色为黑色,半径为 6px,需要使用 `transform: translate(-50%, -50%);` 属性将它定位在钟表屏幕的正中心。 4. 在 JavaScript 中编写函数 `updateClock()`,用于更新钟表屏幕的显示。该函数首先获取当前时间,然后根据时、分、的值计算出时针、分针和针的旋转角度,使用 `transform: rotate()` 属性将它们旋转到正确的位置。 5. 在 JavaScript 中使用 `setInterval()` 方法,每钟调用一次 `updateClock()` 函数,实现钟表屏幕的动画效果。 注意:以上代码只是一个简单的示例,实际应用中还可以根据需求添加更多的功能和效果

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值