用html+css+javascript制作圆形时钟

  制作好的效果如下:

  表盘、刻度和数字全都是用html+css完成的。

  html+css代码如下:

<!DOCTYPE html>
<html>
<head>
	<title>圆形时钟</title>
	<style type="text/css">
		#div1{
			width: 500px;
			height: 500px;
			border:1px solid black;
			border-radius: 50%;
			margin: auto;
			position: relative;
		}
		.clockPanel{
			width: 500px;
			border-top: 1px solid black;
			position: absolute;
			margin-top: 250px;
			transform: rotate(0deg);
		}
		#div2{
		width: 470px;
		height: 470px;
		border-radius: 50%;	
		background-color: white;
		position: absolute;
		margin: 15px;
		}
		.shuzi{
		width: 10px;
		height: 470px;
		position: absolute;
		margin-left: 245px;
		margin-top: 15px;
		}
		#div3{
			width: 30px;
			height: 30px;
			border-radius: 50%;
			background-color: blue;
			position: absolute;
			margin: 235px;
		}
		#hourPoint{
			height: 300px;
			position: absolute;
			margin-left: 250px;
			margin-top: 100px;
			transform: (0deg);

		}		
		#hourPoint div{
			height: 150px;
			border-left: 8px solid black;
			position: absolute;
			margin-left: -8px;
			transform: (0deg);

		}
		#minutePoint {
			height: 360px;
			position: absolute;
			margin-left: 250px;
			margin-top: 70px;
			transform: rotate(0deg);

		}
		#minutePoint div{
			height: 180px;
	 		border-left: 5px solid black;
			position: absolute;
			margin-left: -5px;
			transform: rotate(0deg);

		}
		#secondPoint{
			height: 420px;		
			position: absolute;
			margin-left: 250px;
			margin-top: 40px;
			transform: rotate(0deg);

		}
		#secondPoint div{
			height: 210px;
			border-left: 2px solid black;
			position: absolute;
			margin-left: -2px;
		}
		p{
			font: 20px bold;
		}
	</style>
</head>
<body>
<div id="div1">
	<div id="div2"></div>
	<div class="shuzi"><p>12</p><p style="margin-top: 390px;">6</p></div>
	<div class="shuzi" style="transform: rotate(30deg);"><p style="transform: rotate(-30deg);">1</p><p style="margin-top: 390px; transform: rotate(-30deg);">7</p></div>
	<div class="shuzi" style="transform: rotate(60deg);"><p style="transform: rotate(-60deg);">2</p><p style="margin-top: 390px; transform: rotate(-60deg);">8</p></div>
	<div class="shuzi" style="transform: rotate(90deg);"><p style="transform: rotate(-90deg);">3</p><p style="margin-top: 390px;transform: rotate(-90deg);">9</p></div>
	<div class="shuzi" style="transform: rotate(120deg);"><p style="transform: rotate(-120deg);">4</p><p style="margin-top: 390px;transform: rotate(-120deg);">10</p></div>
	<div class="shuzi" style="transform: rotate(150deg);"><p style="transform: rotate(-150deg);">5</p><p style="margin-top: 395px;transform: rotate(-150deg);">11</p></div>
	<div id="hourPoint">
		<div></div>
	</div>
	<div id="minutePoint">
		<div></div>
	</div>
	<div id="secondPoint">
		<div></div>
	</div>
	<div id="div3"></div>
</div>
<script type="text/javascript" src="clock.js"></script>
</body>
</html>

  js代码如下:

var minute;
var hour;
var second;
function createClock(){
	var circle=document.getElementById("div1");
	var smallcircle=document.getElementById("div2");
	var angle=0;
	for(var i=0;i<30;i++){
		var clockPanel=document.createElement("div");
		//clockPanel.setAttribute("width","500px");
		//clockPanel.style.borderTop="1px solid black"
		clockPanel.setAttribute("class","clockPanel");
		circle.appendChild(clockPanel); 
		circle.insertBefore(clockPanel,smallcircle);   
		clockPanel.style.transform="rotate("+angle+"deg)";
		angle+=6; 
		if(i%5==0){
			clockPanel.style.border="2px solid black";
		}
	}
}

function getDate(){
	 var date=new Date();
	 hour=date.getHours();
	 minute=date.getMinutes();
	 second=date.getSeconds(); 
}
var angle1=6;
var angle2=6;
var angle3=6;
function secondPointFresh(){
	//alert("dhf");
	//alert(secondPoint.style.transform);
	second++;
	if(second==60){
		second=0;
		minute++;
		if(minute==60){
			minute=0;
			hour++;
			if(hour==12)
				hour=0;
        }
	}
	angle1=second*6;
	var secondPoint=document.getElementById("secondPoint");
	secondPoint.style.transform="rotate("+angle1+"deg)";
	angle2=(minute*6);
	var minutePoint=document.getElementById("minutePoint");
	minutePoint.style.transform="rotate("+angle2+"deg)";
	angle3=((hour%12)*30)+(minute*0.5);
	var hourPoint=document.getElementById("hourPoint");
	hourPoint.style.transform="rotate("+angle3+"deg)";
}

createClock();
getDate();
secondPointFresh();
setInterval("secondPointFresh()",1000);

  

转载于:https://www.cnblogs.com/njzy8856/p/8462027.html

动态时钟 body,div,p{ font-family: 'Microsoft Yahei' ;font-size: 14px;} .box{ width: 400px; height: 400px; border:10px solid #8bf2f1;margin:100px auto; border-radius: 50%; box-shadow: 0px 0px 20px 3px #444 inset; position: relative;} /*原点*/ .origin{ width: 20px; height: 20px; border-radius: 50%; background: #ff0000; top:190px; left: 190px; position: absolute;} /* 指针 */ .clock_line{ position: absolute;position:absolute;z-index:20;} .hour_line{width:100px;height:4px;top:198px;left:200px;background-color:#000;border-radius:2px; transform-origin:0 50%;box-shadow:1px -3px 8px 3px #aaa;} .minute_line{width:130px;height:2px;top:199px;left:190px;background-color:#000; transform-origin:7.692% 50%;box-shadow:1px -3px 8px 1px #aaa;} .second_line{width:170px;height:1px;top:199.5px;left:180px;background-color:#f60; transform-origin:11.765% 50%;box-shadow:1px -3px 7px 1px #bbb;} .dot_box{width: inherit; height: inherit;} /*时钟数*/ .dot{ width: 40px; height: 40px; line-height: 40px; text-align: center; font-size: 22px; position: absolute;} .clock-scale{width:195px;height:2px;transform-origin:0% 50%;z-index:7; position:absolute;top:199px;left:200px;} .scale-show{ width:12px;height:2px;background-color:#555;float:left;} .scale-hidden{width:183px;height:2px;float:left;} /*日期*/ .date_info{width:160px;height:28px; line-height:28px;text-align:center;position:absolute;top:230px;left:120px;z-index:11;color:#555; font-weight:bold;} .time_info{ width: 110px; height: 35px; line-height: 35px;text-align:center;position:absolute;top:270px;left:150px;z-index:11;color:#555; background: #253e3e; } .time{ width: 35px ;height:35px; float: left; color: #fff; font-size: 22px;} #minute_time{border-left:1px solid #fff;border-right:1px solid #fff;} <div class
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值