<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#a{width:300px;height: 200px;border:1px solid black;margin:200px auto;font-size: 18px;;text-align: center;line-height: 200px;}
</style>
<script>
/*
写一个定时器,每个一秒修改一次div内文本颜色和文字大小.
最开始这个文字是默认大小,大小开始增大,当增大了6次以后,
文字大小开始缩小,缩小6次,文字再开始增大。
*/
function getStyle(node, cssStyle){ //取出内部样式中的style 考虑浏览器的兼容
return node.currentStyle ? node.currentStyle[cssStyle] : getComputedStyle(node)[cssStyle];
}
window.onload =function (){
var i =3; //每秒钟增大的字码
var count = 0; //记录次数
var oDiv = document.getElementById("a"); //取出div
var font = parseInt(getStyle(oDiv,'fontSize')); //取出样式
setInterval( function (){
//颜色改变
oDiv.style.color = "rgb(" + parseInt(Math.random()*256) + "," + parseInt(Math.random()*256) + "," + parseInt(Math.random()*256) + ")"; //随机颜色
oDiv.style.fontSize = font + i + "px"; //每一秒加3
font+=i;//把加过之后的字体大小加上去
count++; //次数加1
if(count%6 == 0){ //每加了6次就取反
i*=-1;
}
},1000)
}
</script>
</head>
<body>
<div id="a">啊啊啊</div>
</body>
</html>