HTML时钟

@TOC2021年寒假见习练习

HTML时钟

2021年寒假见习

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script >
			//document.createElement();
			//item.style.cssText=""
			//appendChild()
			
			//绘制表盘
			var item=document.createElement("div");
				item.style.cssText=`
				width:200px;
				height:200px;
				border:1px solid #000;
				margin:0 auto;
				border-radius:50%;
				position:relative;
			`;
			document.body.appendChild(item);
			
			//document.body.createElement("div");
			
			//绘制刻度
			for(var i=0;i<60;i++){
				var mark=document.createElement("div");
					mark.style.cssText=`
					width:4px;
					height:${i%5===0?10:5}px;
					background:#000;
					position:absolute;
					left:98px;
					top:0;
					transform-origin:center 100px;
					transform:rotate(${i*6}deg)
				`;
				item.appendChild(mark);
			}
			
			
			//时针,分针,秒针的制作
			//秒针
			var secondEle=document.createElement("div");
			secondEle.style.cssText=`
			width:80px;
			height:2px;
			background:red;
			position:absolute;
			left:100px;
			top:99px;
			transform-origin:left center;
			`;
			item.appendChild(secondEle);
			//分针
			var minuteEle=document.createElement("div");
			minuteEle.style.cssText=`
			width:70px;
			height:4px;
			background:yellow;
			position:absolute;
			left:100px;
			top:98px;
			transform-origin:left center;
			`;
			item.appendChild(minuteEle);
			//时针
			var hourEle=document.createElement("div");
			hourEle.style.cssText=`
			width:50px;
			height:6px;
			background:#000;
			position:absolute;
			left:100px;
			top:97px;
			transform-origin:left center;
			`;
			item.appendChild(hourEle);
			//指针的转动
			setInterval(function() {
				var date=new Date();
				var hours=date.getHours();
				var minutes=date.getMinutes();
				var seconds=date.getSeconds();
				hourEle.style.transform=`rotate(${hours*30+minutes*0.5-90}deg)`;
				minuteEle.style.transform=`rotate(${minutes*6-90}deg)`;
				secondEle.style.transform=`rotate(${seconds*6-90}deg)`;
			},1000)
			
			//数字
			var numbers=[3,4,5,6,7,8,9,10,11,12,1,2];
			var center=document.createElement("div");
			center.style.cssText=`
				width:2px;
				height:2px;
				position:absolute;
				left:99px;
				top:99px;
			`;
			item.appendChild(center);
			
			for(var i=0;i<12;i++){
				var div=document.createElement("div");
				var left=Math.cos(i*Math.PI/6)*80-10;
				var t=Math.sin(i*Math.PI/6)*80-10;
				div.style.cssText=`
					width:20px;
					height:20px;
					position:absolute;
					left:${left}px;
					top:${t}px;
					text-align:center;
					line-height:20px;
				`;
				div.innerHTML=numbers[i];
				center.appendChild(div);
			}
		</script>
	</body>
</html>

效果图

重点知识
javascript中的变量如何声明
使用var可以声明变量
javascript中的流程控制有哪些结构
单路分支
If(){}
双路分支
If(){}else{}
多路分支
If(){}else if(){}else{}
循环结构
for(var i=0;i<10;i++){}
什么是数组
数组就是存储一系列值的结构 [1,2,3,4] [“张三”,”李四”]
什么是对象
对象就是现实中具体的物体和抽象的规则在程序世界中的体现 {name:”王琦”,major:”工程师”,age:18}

获取一个随机整数的方式
Math.ceil(Math.random()*n);
javascript中的定时器函数是?
setInterval();
设置一个元素的内容通过什么方式?
innerHTML

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 HTML 时钟示例: ```html <!DOCTYPE html> <html> <head> <title>HTML 时钟</title> <style> .clock { display: block; margin: 50px auto; width: 200px; height: 200px; border-radius: 50%; border: 10px solid #333; position: relative; } .hour-hand { position: absolute; top: 50%; left: 50%; transform-origin: bottom; height: 40px; width: 6px; background: #333; margin-left: -3px; } .minute-hand { position: absolute; top: 50%; left: 50%; transform-origin: bottom; height: 60px; width: 4px; background: #333; margin-left: -2px; } .second-hand { position: absolute; top: 50%; left: 50%; transform-origin: bottom; height: 80px; width: 2px; background: red; margin-left: -1px; z-index: 1; } .dot { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 12px; height: 12px; border-radius: 50%; background: #333; z-index: 2; } </style> </head> <body> <div class="clock"> <div class="hour-hand"></div> <div class="minute-hand"></div> <div class="second-hand"></div> <div class="dot"></div> </div> <script> function updateClock() { let now = new Date(); let hour = now.getHours(); let minute = now.getMinutes(); let second = now.getSeconds(); let hourHand = document.querySelector(".hour-hand"); let minuteHand = document.querySelector(".minute-hand"); let secondHand = document.querySelector(".second-hand"); let hourDegree = hour * 30 + minute * 0.5; let minuteDegree = minute * 6; let secondDegree = second * 6; hourHand.style.transform = `rotate(${hourDegree}deg)`; minuteHand.style.transform = `rotate(${minuteDegree}deg)`; secondHand.style.transform = `rotate(${secondDegree}deg)`; } setInterval(updateClock, 1000); </script> </body> </html> ``` 这个时钟由一个圆形的表盘和三个时针组成,使用 CSS 进行样式设置,并使用 JavaScript 每秒更新时针的旋转角度。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值