JavaScript实现动态时钟显示

目录

动态时钟显示效果

 代码实现

1.创建html文件(时钟显示.html)

2.设置html标签

3.设置html标签的CSS样式

4.设置JavaScript

1)创建函数和Date

2)获取date变量中的年、月、日,拼接成日期

3)获取date变量中的小时、分钟、秒和日期,拼接成时间

4)获取节点并向节点中添加日期和时间

5)调用自定义函数dateTime和setInterval函数,实现动态时钟显示效果


动态时钟显示效果

 代码实现

1.创建html文件(时钟显示.html)

2.设置html标签

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>动态时钟显示</title>
	</head>
	<body>
		<div class="div">
			<div class="firstDiv"></div>
			<div class="lastDiv"></div>
		</div>
	</body>
</html>

3.设置html标签的CSS样式

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>动态时钟显示</title>
        <style>
			.div{
				/* 宽度400 */
				width: 400px;
				/*高度400  */
				height: 400px;
				/* 设置背景色 */
				background-color: cornflowerblue;
				/* 设置边框圆角200 */
				border-radius: 200px;
				/* 设置字体粗细700 */
				font-weight: 700;
				/* 设置文本颜色白色 */
				color: white;
				/* 设置外边距:上下100px,左右剧中  */
				margin: 100px auto;
			}
			.firstDiv{
				/* 设置宽度300 */
				width: 300px;
				/* 设置高度100 */
				height: 100px;
				/* 设置行高100 */
				line-height: 100px;
				/* 设置内容剧中显示 */
				text-align: center;
				/* 设置字体大小30 */
				font-size: 30px;
				/* 设置相对定位 */
				position: relative;
				/* 设置顶端移动100 */
				top: 100px;
				/* 设置左移动50 */
				left: 50px;
			}
			.lastDiv{
				/* 设置宽度300 */
				width: 300px;
				/* 设置高度100 */
				height: 100px;
				/* 设置内容剧中显示 */
				text-align: center;
				/* 设置字体大小30 */
				font-size: 30px;
				/* 设置相对定位 */
				position: relative;
				/* 设置顶端移动100 */
				top: 100px;
				/* 设置左移动50 */
				left: 50px;
			}
		</style>
	</head>
	<body>
		<div class="div">
			<div class="firstDiv"></div>
			<div class="lastDiv"></div>
		</div>
	</body>
</html>

4.设置JavaScript

1)创建函数和Date

        Date返回的是中国标准时间:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>动态时钟显示</title>
        <style>
			.div{
				/* 宽度400 */
				width: 400px;
				/*高度400  */
				height: 400px;
				/* 设置背景色 */
				background-color: cornflowerblue;
				/* 设置边框圆角200 */
				border-radius: 200px;
				/* 设置字体粗细700 */
				font-weight: 700;
				/* 设置文本颜色白色 */
				color: white;
				/* 设置外边距:上下100px,左右剧中  */
				margin: 100px auto;
			}
			.firstDiv{
				/* 设置宽度300 */
				width: 300px;
				/* 设置高度100 */
				height: 100px;
				/* 设置行高100 */
				line-height: 100px;
				/* 设置内容剧中显示 */
				text-align: center;
				/* 设置字体大小30 */
				font-size: 30px;
				/* 设置相对定位 */
				position: relative;
				/* 设置顶端移动100 */
				top: 100px;
				/* 设置左移动50 */
				left: 50px;
			}
			.lastDiv{
				/* 设置宽度300 */
				width: 300px;
				/* 设置高度100 */
				height: 100px;
				/* 设置内容剧中显示 */
				text-align: center;
				/* 设置字体大小30 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值