JS钟表特效

<!DOCTYPE html>
<html>

<head lang="en">
    <meta charset="UTF-8">
    <title>钟表特效</title>
    <style>
        .clock {
            width: 600px;
            height: 600px;
            margin: 0 auto;
            background: url(images/clock.jpg) no-repeat;
            position: relative;
        }

        .clock div {
            width: 100%;
            height: 100%;
            position: absolute;
            top: 0;
            left: 0;

        }

        .h {
            background: url(images/hour.png) no-repeat center center;
        }

        .m {
            background: url(images/minute.png) no-repeat center center;
        }

        .s {
            background: url(images/second.png) no-repeat center center;
        }
    </style>
</head>

<body>
    <div class="clock">
        <div class="h" id="hour"></div>
        <div class="m" id="minute"></div>
        <div class="s" id="second"></div>
    </div>
</body>

</html>
<script>
    var hour = document.getElementById("hour");
    var minute = document.getElementById("minute");
    var second = document.getElementById("second");
    // 开始定时器
    var s = 0, m = 0, h = 0, ms = 0;
    setInterval(function () {
        var date = new Date();  // 得到最新的时间
        ms = date.getMilliseconds(); // 现在的毫秒数
        s = date.getSeconds() + ms / 1000;  //  得到秒 1.3 s
        m = date.getMinutes() + s / 60;  //  得到的是分数  45.6分钟
        h = date.getHours() % 12 + m / 60;
        //旋转角度
        // 一圈  360 °     一共有 60 秒       每秒  6 °   现在是 s秒
        second.style.WebkitTransform = "rotate(" + s * 6 + "deg)";
        //  变化旋转deg度
        minute.style.WebkitTransform = "rotate(" + m * 6 + "deg)";
        hour.style.WebkitTransform = "rotate(" + h * 30 + "deg)";
        second.style.MozTransform = "rotate(" + s * 6 + "deg)";
        //  变化旋转de度
        minute.style.MozTransform = "rotate(" + m * 6 + "deg)";
        hour.style.MozTransform = "rotate(" + h * 30 + "deg)";
    }, 100);
</script>
  1. images/minute.png
    在这里插入图片描述
  2. images/hour.png
    在这里插入图片描述
  3. images/second.png
    在这里插入图片描述
  4. images/clock.jpg
    在这里插入图片描述
  • 26
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个基本的JavaScript圆形钟表示例: HTML: ``` <div id="clock" style="width:200px;height:200px"></div> ``` JavaScript: ``` function drawClock() { var canvas = document.getElementById("clock"); var ctx = canvas.getContext("2d"); var radius = canvas.height / 2; ctx.translate(radius, radius); radius = radius * 0.90; drawFace(ctx, radius); drawNumbers(ctx, radius); drawTime(ctx, radius); } function drawFace(ctx, radius) { var gradient; ctx.beginPath(); ctx.arc(0, 0, radius, 0, 2*Math.PI); ctx.fillStyle = 'white'; ctx.fill(); gradient = ctx.createRadialGradient(0,0,radius*0.95, 0,0,radius*1.05); gradient.addColorStop(0, '#333'); gradient.addColorStop(0.5, 'white'); gradient.addColorStop(1, '#333'); ctx.strokeStyle = gradient; ctx.lineWidth = radius*0.1; ctx.stroke(); ctx.beginPath(); ctx.arc(0, 0, radius*0.1, 0, 2*Math.PI); ctx.fillStyle = '#333'; ctx.fill(); } function drawNumbers(ctx, radius) { var ang; var num; ctx.font = radius*0.15 + "px arial"; ctx.textBaseline="middle"; ctx.textAlign="center"; for(num = 1; num < 13; num++){ ang = num * Math.PI / 6; ctx.rotate(ang); ctx.translate(0, -radius*0.85); ctx.rotate(-ang); ctx.fillText(num.toString(), 0, 0); ctx.rotate(ang); ctx.translate(0, radius*0.85); ctx.rotate(-ang); } } function drawTime(ctx, radius){ var now = new Date(); var hour = now.getHours(); var minute = now.getMinutes(); var second = now.getSeconds(); //hour hour=hour%12; hour=(hour*Math.PI/6)+ (minute*Math.PI/(6*60))+ (second*Math.PI/(360*60)); drawHand(ctx, hour, radius*0.5, radius*0.07); //minute minute=(minute*Math.PI/30)+(second*Math.PI/(30*60)); drawHand(ctx, minute, radius*0.8, radius*0.07); // second second=(second*Math.PI/30); drawHand(ctx, second, radius*0.9, radius*0.02); } function drawHand(ctx, pos, length, width) { ctx.beginPath(); ctx.lineWidth = width; ctx.lineCap = "round"; ctx.moveTo(0,0); ctx.rotate(pos); ctx.lineTo(0, -length); ctx.stroke(); ctx.rotate(-pos); } drawClock(); ``` 这会生成一个简单但吸引人的钟表。请注意,它并没有实时更新时间 - 您需要在JavaScript中添加逻辑,以使它实时更新。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值