<!DOCTYPE html>
<html>
<head>
<title>改变颜色</title>
<meta charset="utf-8">
<style type="text/css">
#bordercolor{
width: 400px;
height: 400px;
margin: 0 auto;
}
</style>
</head>
<body>
<div id="bordercolor"></div>
<div style="margin: 20px auto;width: 150px">
<input type="button" name="" value="+" onclick="add(1);">
<input type="button" name="" value="开始" onclick="begin();">
<input type="button" name="" value="-" onclick="add(0);">
</div>
<script type="text/javascript">
var tar;
var speed = 1000;
function changeColor(){
document.getElementById("bordercolor").style.backgroundColor = randomColor();
}
function randomColor(){
var arr = [0,1,2,3,4,5,6,7,8,9,"A","B","C","D","E","F"];
var restcolor = "#";
for(var i = 0; i < 6; i++){
restcolor = restcolor + arr[Math.floor(Math.random() * 16)];
}
return restcolor;
}
function begin(){
tar = setInterval(changeColor,speed);
}
function add(player){
if (player == 1) {
clearInterval(tar);
speed = speed + 1000;
tar = setInterval(changeColor,speed);
}else if (player == 0) {
clearInterval(tar);
speed = speed - 1000;
tar = setInterval(changeColor,speed);
}
}
</script>
</body>
</html>
随机颜色的函数,一个借的网上的,一个自己用数组写的。自己写的不好看,网上的更简洁。