js onmousedown onmouseup 实现鼠标按下自动变色,鼠标抬起回复原来背景颜色

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		
	</head>
	<body>
		<div style="width: 400px;height: 400px;background: red;" id="box"></div>
		<script type="text/javascript">
			var box = document.getElementById('box')
			var colors = ['green','orange','yellow','black']
			var i = 0
			
			function bh(){
     
				if(i == colors.length){
     
					i=0
				}
				box.style.backgroundColor = colors[i]
				i++
				
			}
			box.onmousedown = function(){
     

				begin = window.setInterval('bh()',1000)
				
			}
			box.onmouseup = function(){
     
				box.style.backgroundColor = 'blue'
				clearInterval(begin)
				
			}
		</script>
	</body>
</html>

你可能感兴趣的:(HTML)