学习笔记-HTML+CSS+JavaScript简易秒表

简单实现了一个有开始、暂停和归零的秒表

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		* {
			user-select: none;
		}
		#kuai {
			position: absolute;
			top: 40%;
			left: 50%;
			transform: translate(-50%, -50%);
            width: 200px;
            height: 130px;
            background-color: rgb(50, 150, 100);
            border-radius: 3px;
            outline: none;
			margin: 1pt;
			color: white;
			text-align: center;
			line-height: 130px;
			font-size: 40px;
			font-weight: 800;
			
		}
		#container {
			position: absolute;
			top: 50%;
			left: 50%;
			transform: translate(-50%, -50%);
            width: 230px;
            height: 200px;
			background-color: #aaa;
			
		}
		#zanting {
			position: absolute;
			top: 75%;
			left: 15px;
			width: 90px;
            height: 30px;
			background-color: #fff;
			text-align: center;
			line-height: 30px;
			border-radius: 5px;
			cursor: pointer;
		}
		#guiling {
			position: absolute;
			top: 75%;
			right: 15px;
			width: 90px;
            height: 30px;
			background-color: #fff;
			text-align: center;
			line-height: 30px;
			border-radius: 5px;
			cursor: pointer;
		}
		#kaishi {
			position: absolute;
			left: 50%;
			bottom: 0;
			transform: translate(-50%);
			right: 15px;
			width: 90px;
            height: 30px;
            background-color: rgb(50, 150, 100);
			text-align: center;
			line-height: 30px;
			border-radius: 15px;
			cursor: pointer;
			color: white;
		}
		#zanting:hover {
			background-color: rgb(230, 230, 230);
		}
		#guiling:hover {
			background-color: rgb(230, 230, 230);
		}
		#kaishi:hover {
			background-color: rgb(39, 126, 83);
		}
	</style>
</head>
<body>
	<div id="container">
	<div id="kuai"></div>
	<div id="zanting" onclick="stop()">暂停</div>
	<div id="guiling" onclick="back()">归零</div>
	<div id="kaishi" onclick="start()">开始</div>
	</div>
	<script>
	var flag = false;
	var count = 0;
	var kuai = document.getElementById('kuai');
	kuai.innerHTML = '0.00';
	function number() {
		if (flag)
    	{count +=0.01;
    	var ans = parseInt(count*100)/100;
    	var newAns = ans.toString();
    	pos = newAns.indexOf('.');
    	if (newAns.length - pos < 3) {
        	newAns += '0';
    	}
    	kuai.innerHTML = newAns;}
	}
	var timer = setInterval(number,10);
	function stop() {
    	flag = false;
	}
	function back() {
    	stop()
		count = 0;
    	kuai.innerHTML = '0.00';
	}
	function start() {
    	flag = true;
	}
	</script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值