用javascript设计时钟

用javascript设计时钟

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>clock</title>
		<link rel="stylesheet" type="text/css" href="css/clock.css"/>
		<script type="text/javascript" src="js/clock.js"></script>
	</head>
	<body>
		<div id="clock"></div>
		<div id="hours"></div>
		<div id="minutes"></div>
		<div id="seconds"></div>
	</body>
</html>
*{
	margin: 0;
	padding: 0;
}
body{
	margin: auto;
	width: 500px;
	height: 500px;
	/*border: 2px solid red;*/
	position: relative;
}
#clock{
	width: 240px;
	height: 240px;
    border: 3px solid black;
	border-radius: 50%;
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%,-50%);
}
/*表盘上的刻度*/
span{
	display:block;
	width: 40px;
	height: 40px;
	font-size: 60px;
    color: black;
    text-align: center;
    line-height: 0px;
    /*border: 2px solid red;*/
   /* border-radius: 50%;*/
   position: absolute;
   transform:translate(-50%,-50%);
}
span:nth-child(1),span:nth-child(4),span:nth-child(9),span:nth-child(12){
	font-size: 40px;
	line-height: 40px;
}
span:nth-child(1){
	top: 50%;
	left: 0%;
}
span:nth-child(2){
	top: 33.33%;
	left: 16.67%;
	transform:translate(-100%,-100%) rotate(45deg);
}
span:nth-child(3){
	top: 16.67%;
	left: 33.33%;
	transform:translate(-100%,-100%) rotate(45deg);
}
span:nth-child(4){
	top: 0%;
	left: 50%;
}
span:nth-child(5){
	top:16.67%;
	left: 66.67%;
	transform:translate(0%,-100%) rotate(-45deg);
}
span:nth-child(6){
	top: 33.33%;
	left: 83.3%;
	transform:translate(0%,-100%) rotate(-45deg);
}
span:nth-child(7){
	top: 66.67%;
	left: 16.67%;
	transform:translate(-100%,0%) rotate(-45deg);
}
span:nth-child(8){
	top: 83.33%;
	left: 33.33%;
	transform:translate(-100%,0%) rotate(-45deg);
}
span:nth-child(9){
    top: 100%;
    left: 50%;
}
span:nth-child(10){
	top: 83.33%;
	left:66.67%;
	transform:translate(0%,0%) rotate(45deg);
}
span:nth-child(11){
	top: 66.67%;
	left: 83.33%;
	transform:translate(0%,0%) rotate(45deg);
}
span:nth-child(12){
	top: 50%;
	left: 100%;
}
/*表盘上的指针
 * 1.将指针设置成一个原点
 * 2.分别给时针,分针,秒针添加伪类before;
 * */
#hours,#minutes,#seconds{
	width: 10px;
	height: 10px;
	border-radius: 50%;
	background: black;
	/*给指针定位*/
	position: absolute;
	top: 50%;
	left: 50%;
	transition: all;
}
#hours:before,#minutes:before,#seconds:before{
	content: "";
	display: block;
	position: absolute;
}
#hours:before{
	width: 6px;
	height: 50px;
	background: red;
	top: -50px;
	left: 2px;
}
#minutes:before{
	width: 4px;
	height: 80px;
	background: green;
	top: -80px;
	left: 3px;
}
#seconds:before{
	width: 2px;
	height: 100px;
	background: blue;
	top: -100px;
	left: 4px;
}

window.onload=function(){
	/*获得盒子clock的id*/
	var clock=document.getElementById("clock");
	/*在clock盒子里面追加12个span*/
	var num=8;/*表盘数字*/
	for(let i=14;i>2;i--){  
		if(i>8){/*上表盘的上的刻度*/
			num++;
		}else{
			num=i;/*下表盘的上的刻度*/
		}
		/*追加元素节点span*/
		var text=document.createElement("span");
		if(num%3==0){
			text.innerText=num;
		}else{
			text.innerText=".";
		}
	    clock.appendChild(text);
	}
	time();
	setInterval(time,1000);
	function time(){
		/*获取当前时间对象*/
		var myDate = new Date();
		var hours=parseInt(myDate.getHours());
		/*alert(hours);*/
		var minutes=parseInt(myDate.getMinutes());
		/*alert(minutes);*/
		var seconds=parseInt(myDate.getSeconds());
		/*alert(seconds);*/
		
		var  hoursMove=document.getElementById("hours");
		var  minutesMove=document.getElementById("minutes");
		var  secondsMove=document.getElementById("seconds");
	        
        secondsMove.style.transform= "translate(-50%,-50%)"+"rotate("+6*(seconds)%360+"deg)"; 
        minutesMove.style.transform= "translate(-50%,-50%)"+"rotate("+6*minutes+"deg)"; 
        hoursMove.style.transform= "translate(-50%,-50%)"+"rotate("+30*(Math.abs(hours-12)+minutes/60)+"deg)"; 	
	}
    clearInterval(time);
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值