圆形钟表js实现

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
/* 样式 */
*{
padding: 0;
margin: 0;
}
#clock{
width: 200px;
height: 200px;
border: 2px solid black;
border-radius: 100px;
position: absolute;
margin-left: 20px;
margin-top:20px;
}
#a12{
position: absolute;
top:0;
left:90px;
}
#a6{
position: absolute;
top:180px;
left:90px;
}
#a3{
position: absolute;
top:90px;
left:180px;
}
#a9{
position: absolute;
top:90px;
left:0px;
}
#a1{
position: absolute;
top:10px;
left:145px;
}
#a2{
position: absolute;
top:45px;
left:175px;
}
#a5{
position: absolute;
top:165px;
left:145px;
}
#a4{
position: absolute;
top:135px;
left:170px;
}
#a11{
position: absolute;
top:10px;
left:45px;
}
#a10{
position: absolute;
top:45px;
left:15px;
}
#a8{
position: absolute;
top:135px;
left:15px;
}
#a7{
position: absolute;
top:170px;
left:45px;
}
#h{
width:7px;
height: 50px;
background-color: #37639C;
position: absolute;
border-radius: 6px;
left: 97px;
top: 50px;
}
#m{
width:5px;
height: 65px;
background-color: #37639C;
position: absolute;
top: 35px;
left: 98px;
border-radius: 4px;
}
#s{
width:3px;
height: 70px;
background-color: #37639C;
position: absolute;
top: 25px;
left: 99px;
border-radius: 2px;
}
#s,#m,#h{
transform-origin: center bottom;
}
</style>
</head>
<body>
<div id="clock">
<div id="h"></div>
<div id="m"></div>
<div id="s"></div>
<div id="a1">I</div>
<div id="a2">II</div>
<div id="a3">III</div>
<div id="a4">IV</div>
<div id="a5">V</div>
<div id="a6">VI</div>
<div id="a7">VII</div>
<div id="a8">VIII</div>
<div id="a9">IX</div>
<div id="a10">X</div>
<div id="a11">XI</div>
<div id="a12">XII</div>
</div>
<script>
var divS=document.getElementById("s");
var divM=document.getElementById("m");
var divH=document.getElementById("h");
/*时、分、秒针转动度数计算*/
function task(){
var now=new Date();
var s=now.getSeconds();
var sDeg=s/60*360;
var m=now.getMinutes();
var mDeg=(m*60+s)/3600*360;
var h=now.getHours();
var hDeg=(h*3600+m*60+s)/(3600*12)*360;
divS.style.transform=`rotate(${sDeg}deg)`;
divM.style.transform=`rotate(${mDeg}deg)`;
divH.style.transform=`rotate(${hDeg}deg)`;
}
task();
/*事件间隔事件*/
setInterval(task,1000);
</script>
</body>
</html>
好的,以下是一个基本的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、付费专栏及课程。

余额充值