div {
width: 50px;
height: 50px;
border-radius: 50%;
position: absolute;
text-align: center;
font-size: 30px;
color: #fff;
}
//让第一个盒子直接跟随鼠标位置
//后面的盒子每隔几毫米跟随前一个盒子变化位置
window.onload = function(){
var allDiv = document.getElementsByTagName("div");//div数组
document.onmousemove = function(e){
e = e || window.event;//兼容性
allDiv[0].style.top = e.clientY + "px";
allDiv[0].style.left = e.clientX + "px";
}
//设置定时器
setInterval(function(){
for(var i=allDiv.length-1;i>0;i--){
allDiv[i].style.top = allDiv[i-1].style.top;
allDiv[i].style.left = allDiv[i-1].style.left;
allDiv[i].style.backgroundColor = randomColor();
}
},20)
//设置随机颜色
function randomColor(){
var str = "0123456789abcdef";
var bgColor = "#";
for(var i = 0;i<6;i++){
var temp = parseInt(str.length*Math.random());
bgColor += temp;
}
return bgColor;
}
}