CSS实现钟表


			/* 定位属性 相对行为relative 绝对定位absolute 固定定位fixed
            /* 相对定位 
            1.是相对于自身原来的位置 
            2.还占据原来的位置 */
            /* position: relative;
            bottom: 10px;
            left: 10px; */
            /* 绝对定位 
            1.相对于有定位属性的父元素 
            2.失去原来位置 */
            /* position: absolute;
            bottom: 10px;
            left: 10px; */
            /* 固定定位
            1.相对于浏览器窗口
            2.失去原来位置*/
            /* 父相子绝 */
            /* position: fixed;
            bottom: 10px;
            right: 10px; */


<!DOCTYPE html>
<html>
<head>
	<title>时钟</title>
</head>
<style type="text/css">
    *{
    	/* 全局变量 设置外边距内边距为0 */
		margin: 0;
		padding: 0;
	}
		/* ul下取消原本样式 */
	ul{
		list-style: none;
	}
	.box{
        /* 设置宽高 */
		width: 500px;
		height: 500px;
        /* 外边距 上下30像素 左右自适应 */
		margin: 30px auto;
        /* 边框弧度50% */
		border-radius: 50%;
        /* 边框 2像素 实线 黑色 */
		border: 2px solid black;
        /* 相对定位 */
		position: relative;
	}
	/* box父类下的ul */
	.box ul{
		width: 100%;height: 100%;
        /* 相对定位 */
		position: relative;
	}
	/*竖线的样式 让旋转角度初始到表的中点*/
	.box ul li{
        /* 设置宽高 */
		width: 4px;
        height: 10px;
        /* 背景色 */
        background-color: black;
        /* 绝对定位 */
        position: absolute;
        /* 下文解读 */
        transform-origin: center 250px; left:50%;
	}
	#hour{
    	/* 宽高属性 */
		width: 12px;
		height: 80px;
		/* 背景色 */
		background: black;
		/* 绝对定位 */
		position: absolute;
		top:50%;
        left: 50%;
        /* 外边框 上 左 下右 */
        margin:-80px 0 0 -6px;
		/* 下文解读 */
		transform-origin: center bottom;
	}
	#minu{
    	/* 宽高属性 */
		width: 8px;
		height: 140px;
		/* 背景色 */
		background: black;
		/* 绝对定位 */
		position: absolute;
		top:50%;
        left: 50%;
        /* 外边框 上 左 下右 */
        margin:-140px 0 0 -4px;
		/* 下文解读 */
		transform-origin: center bottom;
	}
	#sceo{
    	/* 宽高属性 */
		width: 4px;
		height: 180px;
		/* 背景色 */
		background: black;
		/* 绝对定位 */
		position: absolute;
		top:50%;
        left: 50%;
        /* 外边框 上 左 下右 */
        margin:-180px 0 0 -2px;
		/* 下文解读 */
		transform-origin: center bottom;
	}
	#ball{
    	/* 背景色 */
		background: black;
		/* 宽高属性 */
		width: 30px;
		height: 30px;
		/* 绝对定位 */
		position: absolute;
		left: 50%;
		top: 50%;
        /* 外边框 */
		margin: -15px;
		/* 边框弧度 */
		border-radius: 50%;
	}
</style>
<body>
	<div class="box">
		<ul>
			
		</ul>
		<div id = hour></div>
		<div id = minu></div>
		<div id = sceo></div>
		<div id = ball></div>
	</div>
</body>
<script type="text/javascript">
    // 获取上方的值
	var ul = document.querySelector("ul");
	var hour = document.querySelector("#hour");
	var minu = document.querySelector("#minu");
	var sceo = document.querySelector("#sceo");
	// 循环遍历
	for (var i = 0; i < 60; i++) {
		var li = document.createElement("li");
		li.style.transform = 'rotate('+(i*6)+'deg)';
		// 每隔5个生成大的竖线,区分整刻
		if (i%5===0) {
			li.style.height='20px';
		}
		ul.appendChild(li);
	}
	run();
	setInterval(run,1000);
	function run(){
		var date = new Date();
		var h = date.getHours();
		var m = date.getMinutes();
		var s = date.getSeconds();
		console.log(s);
		hour.style.transform = 'rotate('+(h*30+m/2)+'deg)';
		minu.style.transform = 'rotate('+(m*6)+'deg)';
		sceo.style.transform = 'rotate('+(s*6)+'deg)';
	}
</script>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值