<!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>
*{
margin: 0;
padding: 0;
}
div{
width: 400px;
height: 400px;
margin: 0 auto;
border: 1px solid red;
}
#font{
width: 100px;
height: 200px;
margin: 50px auto;
font-size: 20px;
line-height: 200px;
text-align: center;
}
</style>
<script src="../jsTOOL/tool.js"></script>
</head>
<body>
<div id="box">
<p id="font">麒麟大哥</p>
</div>
<script>
document.getElementById('box').style.backgroundColor = getColor()
document.getElementById('font').style.backgroundColor = getColor()
document.getElementById('font').style.color = getColor()
function getColor(){
return 'rgb(' + randomNum(0, 255) + ',' + randomNum(0, 255) + ',' + randomNum(0, 255) + ')'
}
setInterval(function(){
document.getElementById('box').style.backgroundColor = getColor()
document.getElementById('font').style.backgroundColor = getColor()
document.getElementById('font').style.color = getColor()
},100)
</script>
</body>
</html>
<!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>
*{
margin: 0;
padding: 0;
}
div{
width: 400px;
height: 400px;
margin: 0 auto;
border: 1px solid red;
}
#font{
width: 100px;
height: 200px;
margin: 50px auto;
font-size: 20px;
line-height: 200px;
text-align: center;
}
</style>
<script src="../jsTOOL/tool.js"></script>
</head>
<body>
<div id="box">
<p id="font">麒麟大哥</p>
</div>
<script>
document.getElementById('box').style.backgroundColor = getColor()
document.getElementById('font').style.backgroundColor = getColor()
document.getElementById('font').style.color = getColor()
function getColor(){
return 'rgb(' + randomNum(0, 255) + ',' + randomNum(0, 255) + ',' + randomNum(0, 255) + ')'
}
setInterval(function(){
document.getElementById('box').style.backgroundColor = getColor()
document.getElementById('font').style.backgroundColor = getColor()
document.getElementById('font').style.color = getColor()
},100)
</script>
</body>
</html>
封装一个tool工具
/*
*
区间的随机整数
@params { Number} a, b表示数字
@return a, b区间的一个随机整数
*
*/
function randomNum(a, b) {
var min = Math.min(a, b)
var max = Math.max(a, b)
return Math.floor(Math.random() * (max - min + 1)) + min
}