js写一个钟表

在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<style type="text/css">
			* {
				margin: 0px;
				padding: 0px;
			}

			html,
			body {
				height: 100%;
				width: 100%;
				background-color: antiquewhite;
			}

			.wrap {
				width: 500px;
				height: 500px;
				background-color: white;
				margin: 100px auto;
				position: relative;
				border-radius: 50%;
				/* z-index: 10; */
			}

			.wrap .num-wrap {
				width: 100%;
				height: 100%;
				background-color: white;
				border-radius: 50%;
				position: absolute;
				/* z-index: 10; */
			}

			.wrap .num-wrap .num {
				height: 50px;
				width: 50px;
				/* display: block; */
				font-size: 20px;
				text-align: center;
				line-height: 50px;
				/* background-color: rosybrown; */
				position: absolute;
				top: 225px;
				left: 225px;
			}

			.wrap .pointer-wrap {
				width: 100%;
				height: 100%;
				position: absolute;
			}

			/* 旋转中心 x= width/2   y= height-width/2 */
			/* top = 250-(height-width/2) left = 250 - width/2  这里的height和width是指针的宽高*/
			#seconds {
				width: 4px;
				height: 200px;
				background-color: black;
				position: absolute;
				top: 52px;
				left: 248px;
				border-radius: 100px;
				/* transform: rotate(10deg); */
				transform-origin: 2px 198px;
			}

			#min {
				border-radius: 100px;
				width: 8px;
				height: 160px;
				background-color: black;
				position: absolute;
				top: 94px;
				left: 246px;

				/* transform: rotate(10deg); */
				transform-origin: 4px 156px;
			}

			#hours {
				border-radius: 100px;
				width: 10px;
				height: 100px;
				background-color: black;
				position: absolute;
				top: 155px;
				left: 245px;


				transform-origin: 5px 95px;
			}
			.date-wrap{
				height: 30px;
				width: 150px;
				background-color: white;
				position: absolute;
				top: 260px;
				left: 175px;
			    line-height: 30px;
				text-align: center;
				font-size: 18px;
			}
			.day-wrap{
				height: 30px;
				width: 30px;
				background-color: rgb(200,225,225);
				position: absolute;
				top: 320px;
				left: 300px;
				line-height: 30px;
				font-size: 18px;
				text-align: center;
			}
		</style>
	</head>
	<body>
		<div class="wrap">
			<div class="num-wrap">
				<span class="num">12</span>
				<span class="num">1</span>
				<span class="num">2</span>
				<span class="num">3</span>
				<span class="num">4</span>
				<span class="num">5</span>
				<span class="num">6</span>
				<span class="num">7</span>
				<span class="num">8</span>
				<span class="num">9</span>
				<span class="num">10</span>
				<span class="num">11</span>

			</div>
			<div class="pointer-wrap">
				<div class="date-wrap">
					
				</div>
				<div class="day-wrap">
					
				</div>
				<div id="seconds">

				</div>
				<div id="min">

				</div>
				<div id="hours">

				</div>
				
			</div>

		</div>
		<script type="text/javascript">
			var r = 230
			var nums = document.getElementsByClassName('num')
			for (var i = 0; i < nums.length; i++) {
				var y = 0,
					x = 0
				y = -r * Math.cos(i * (Math.PI / 6))
				var x = r * Math.sin(i * (Math.PI / 6))
				nums[i].style.transform = 'translateY(' + y + 'px)' + ' ' + 'translateX(' + x + 'px)'

			}
			var sPointer = document.getElementById('seconds')
			var mPointer = document.getElementById('min')
			var hPointer = document.getElementById('hours')
			var setPonter = function() {
				var now = new Date()
				var h = now.getHours()
				var m = now.getMinutes()
				var s = now.getSeconds()

				sPointer.style.transform = 'rotate(' + 6 * s + 'deg)'

				var mDeg = m / 60 * 360 + s / 60 * 360 / 60
				mPointer.style.transform = 'rotate(' + mDeg + 'deg)'

				var hDeg = (h - 12) * 30 + m / 2
				hPointer.style.transform = 'rotate(' + hDeg + 'deg)'
				
				
			}
			setInterval(function() {
				setPonter()
			}, 100)
			// var date1 = function(){
			// 	var theDate = new Date(theTime)
			// 	var year = theDate.getFullYear()
			// 	var month = theDate.getMonth() + 1
			// 	var date = theDate.getDate()
			// 	var theDate = document.getElementsByClassName('date-wrap')[0]
			// 	var dateStr = year + '年' + month + '月' + date + '日 '
			// 	theDate.innerHTML = dataStr
			// }
			var nowDate =document.getElementsByClassName('date-wrap')[0]
			var nian  =new Date().getFullYear()
			var yue = new Date().getMonth()+1
			var ri = new Date().getDate()
			
			 nowDate.innerHTML= nian+'年'+yue + '月'+ri+'日'
			var nowDay = document.getElementsByClassName('day-wrap')[0]
			var day = new Date().getDay()
			nowDay.innerHTML = day
		</script>
	</body>
</html>

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个基本的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中添加逻辑,以使它实时更新。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值