JS代码制作圆形钟表

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>时钟</title>
  <style>
    #box{
      position: relative;
      width: 400px;
      height: 400px;
      border:26px solid rgb(204, 142, 255);
      border-radius: 50%;
    }
    #box>div{
      position: absolute;
      width: 8px;
      height: 26px;
      background-color: rgb(110, 110, 110);
    }   
    #box>div:nth-child(1){
      width: 16px;
      height: 36px;
      top:0px;
      left: 192px;
    }
    #box>div:nth-child(2){
      top:27px;
      right:98px;
    }    
    #box>div:nth-child(3){
      top:98px;
      right:27px;
    }    
    #box>div:nth-child(4){
      width: 16px;
      height: 36px;
      right:10px;
      top: 182px;
    }
    #box>div:nth-child(5){
      bottom:98px;
      right:27px;
    }    
    #box>div:nth-child(6){
      right:98px;
      bottom:27px;
    }
    #box>div:nth-child(7){
      width: 16px;
      height: 36px;
      bottom:0px;
      left: 192px;
    }
    #box>div:nth-child(8){
      bottom:27px;
      left:98px;
    }    
    #box>div:nth-child(9){
      left:27px;
      bottom:98px;
    }    
    #box>div:nth-child(10){
      width: 16px;
      height: 36px;
      left:10px;
      top: 182px;
      /* 以中心点旋转(宽-高)/10 */
      transform: rotate(90deg);
    }
    #box>div:nth-child(11){
      top:98px;
      left:27px;
    }    
    #box>div:nth-child(12){
      top:27px;
      left:98px;
    }    
    #s{
      position: absolute;
      top:68px;
      left: 194px;
      border-radius: 6px;
      width: 12px;
      height: 132px;
      background-color: rgb(255, 198, 188);
      transform-origin: bottom center;
    }   
    #f{
      position: absolute;
      left:196px;
      top:62px;
      border-radius: 4px;
      width: 8px;
      height: 138px;
      background-color: rgb(255, 128, 128);
      transform-origin: bottom center;
    }    
    #m{
      position: absolute;
      width: 4px;
      height: 144px;
      border-radius: 2px;
      background-color: rgb(243, 117, 117);
      top:56px;
      left: 198px;
      transform-origin: bottom center;
    }
    #circle{
      position:relative;
      top:-426px;
      left:26px
    }
    .c{
      position: absolute;
      left: 176px;
      top:176px;
      border-radius: 50%;
      width: 50px;
      height: 50px;
      background-color: rgb(190, 112, 241)
    }
  </style>
</head>
<body>
  <div id="box">
  </div>  
  <div id="circle">    
  <span id="s"></span>
  <span id="f"></span>
  <span id="m"></span>
  <div class="c"></div>
  </div>
  <script>
    window.onload = function(){
      var box = document.getElementById("box")
      var s = document.getElementById("s")
      var f = document.getElementById("f")
      var m = document.getElementById("m")
      for(var i=0;i<12;i++){
      var div = document.createElement("div")
      div.style.transform = "rotate("+i*30+"deg)"
      div.setAttribute("index",i)
      box.appendChild(div)
      }
      var timer = setInterval(function(){
        var date = new Date();
        var hours = date.getHours()*30+date.getMinutes*0.5+date.getSeconds*0.5/60
        var minute = date.getMinutes()*6+date.getSeconds()*0.1
        var second = date.getSeconds()*6
        s.style.transform = "rotate("+hours+"deg)"
        f.style.transform = "rotate("+minute+"deg)"
        m.style.transform = "rotate("+second+"deg)"
      },1000)
    }
  </script>
</body>
</html>

附上成品图

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用HTML5的canvas元素和JavaScript来制作一个圆形钟表。下面是一个简单的示例代码: ```html <!DOCTYPE html> <html> <head> <style> #canvas { border: 1px solid black; } </style> </head> <body> <canvas id="canvas" width="400" height="400"></canvas> <script> // 获取画布元素 var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); // 设置钟表参数 var radius = canvas.width / 2 - 10; var centerX = canvas.width / 2; var centerY = canvas.height / 2; // 绘制钟表 function drawClock() { // 清除画布 ctx.clearRect(0, 0, canvas.width, canvas.height); // 绘制钟表外圆 ctx.beginPath(); ctx.arc(centerX, centerY, radius, 0, Math.PI * 2); ctx.strokeStyle = "black"; ctx.lineWidth = 2; ctx.stroke(); ctx.closePath(); // 绘制钟表刻度 for (var i = 0; i < 12; i++) { var angle = i * (Math.PI / 6); var x = centerX + radius * Math.cos(angle); var y = centerY + radius * Math.sin(angle); ctx.beginPath(); ctx.arc(x, y, 5, 0, Math.PI * 2); ctx.fillStyle = "black"; ctx.fill(); ctx.closePath(); } // 绘制钟表指针 var now = new Date(); var hour = now.getHours(); var minute = now.getMinutes(); var second = now.getSeconds(); // 绘制时针 var hourAngle = (hour % 12 + minute / 60 + second / 3600) * (Math.PI / 6); drawHand(hourAngle, radius * 0.5, 8, "black"); // 绘制分针 var minuteAngle = (minute + second / 60) * (Math.PI / 30); drawHand(minuteAngle, radius * 0.8, 4, "black"); // 绘制秒针 var secondAngle = (second + now.getMilliseconds() / 1000) * (Math.PI / 30); drawHand(secondAngle, radius * 0.9, 2, "red"); // 更新钟表 setTimeout(function() { requestAnimationFrame(drawClock); }, 1000); } // 绘制钟表指针 function drawHand(angle, length, width, color) { var x = centerX + length * Math.cos(angle - Math.PI / 2); var y = centerY + length * Math.sin(angle - Math.PI / 2); ctx.beginPath(); ctx.moveTo(centerX, centerY); ctx.lineTo(x, y); ctx.strokeStyle = color; ctx.lineWidth = width; ctx.stroke(); ctx.closePath(); } // 启动钟表 drawClock(); </script> </body> </html> ``` 在这个示例中,我们使用`<canvas>`元素创建了一个400x400像素大小的画布,并通过JavaScript获取到了画布的上下文对象`ctx`。然后,我们在`drawClock`函数中使用`ctx`对象来绘制钟表的外圆、刻度和指针。 请注意,这只是一个简单的示例,你可以自行修改代码样式和钟表效果来满足你的需求。希望对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值