跟隨滑鼠一串效果---七彩貪吃蛇
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
#span{
width: 40px;
height: 40px;
border-radius:50%;
border: 1px solid #000;
display:inline-block;
position:absolute;
top:100px;
left:100px;
}
</style>
<script>
window.onload = function(){
var span = document.getElementsByTagName('span');
//定時執行位置的跟蹤,後一個元素接受前一個元素的位置
setInterval(function(){for(i=span.length-1;i>0;i--)
{
span[i].style.left=span[i-1].style.left;
span[i].style.top=span[i-1].style.top;
span[i].style.backgroundColor=span[i-1].style.backgroundColor;
}
},10);
//滑鼠移動事件,元素跟隨滑鼠,並賦予隨機背景
document.onmousemove = function(e){
var str = '0123456789abcdef';var bgColor="#";
for(var i =0;i<6;i++){
var n = parseInt(Math.random()*str.length);
bgColor +=str[n];
}
span[0].style.left = e.clientX + 'px';
span[0].style.top = e.clientY + 'px';
span[0].style.backgroundColor = bgColor;
}
}
</script>
</head>
<body>
<span id='span'></span><span id='span'></span><span id='span'></span><span id='span'></span>
<span id='span'></span><span id='span'></span><span id='span'></span><span id='span'></span>
<span id='span'></span><span id='span'></span><span id='span'></span><span id='span'></span>
<span id='span'></span><span id='span'></span><span id='span'></span><span id='span'></span>
<span id='span'></span><span id='span'></span><span id='span'></span><span id='span'></span>
<span id='span'></span><span id='span'></span><span id='span'></span><span id='span'></span>
<span id='span'></span><span id='span'></span><span id='span'></span><span id='span'></span>
<span id='span'></span><span id='span'></span><span id='span'></span><span id='span'></span>
</body>
</html>