var divs = document.getElementsByTagName("div");
var timerId = setInterval(creatDiv, 500);
var timerId2 = setInterval(gor, 1);
var timerId3 = setInterval(yidong, 1);
var sheArr = new Array();
var she = document.getElementById("se");
sheArr.push(she);
var gwArr = [];
var speedX = -1;
var speedY = -1;
//创建随机的食物
function creatDiv() {
var div = document.createElement("div");
var suiji = suiJi(0, 256);
var suiji2 = suiJi(0, 256);
var suiji3 = suiJi(0, 256);
div.style.background = "rgb(" + suiji + "," + suiji2 + "," + suiji3 + ")";
div.style.width = "20px";
div.style.height = "20px";
div.style.position = "absolute";
div.style.left = suiJi(50, 1200) + "px";
div.style.top = suiJi(10, 600) + "px";
document.body.appendChild(div);
gwArr.push(div);
if(gwArr.length > 5) {
clearInterval(timerId);
}
}
document.onmousemove = function(e) {
var ev = e || event;
if(ev.clientX >= parseInt(sheArr[0].style.left) + 20&&ev.clientY == parseInt(sheArr[0].style.top) + 20) {
speedX = 1;
speedY= 0;
}
if(ev.clientX <= parseInt(sheArr[0].style.left) + 20&&ev.clientY == parseInt(sheArr[0].style.top) + 20) {
speedX = -1;
speedY= 0;
}
if(ev.clientX == parseInt(sheArr[0].style.left) + 20&&ev.clientY >= parseInt(sheArr[0].style.top) + 20) {
speedX = 0;
speedY= 1;
}
if(ev.clientX == parseInt(sheArr[0].style.left) + 20&&ev.clientY <= parseInt(sheArr[0].style.top) + 20) {
speedX = 0;
speedY= -1;
}
if(ev.clientX > parseInt(sheArr[0].style.left) + 20) {
speedX = 1;
}
if(ev.clientX < parseInt(sheArr[0].style.left) + 20) {
speedX = -1;
}
if(ev.clientY > parseInt(sheArr[0].style.top) + 20) {
speedY = 1;
}
if(ev.clientY < parseInt(sheArr[0].style.top) + 20) {
speedY = -1;
}
// sheArr[0].style.left = ev.clientX-20+'px';
// sheArr[0].style.top = ev.clientY-20+'px';
// sheArr[i].style.left = sheArr[i-1].style.left;
// sheArr[i].style.top = sheArr[i-1].style.top;
}
function yidong() {
if(parseInt(sheArr[0].style.left)<=0||parseInt(sheArr[0].style.left)>=document.clientWidth){
speedX *= -1;
}
if(parseInt(sheArr[0].style.top)<=0||parseInt(sheArr[0].style.top)>=document.clientHeight){
speedY *= -1;
}
sheArr[0].style.left = parseInt(sheArr[0].style.left) + speedX*3 + "px";
sheArr[0].style.top = parseInt(sheArr[0].style.top) + speedY*3 + "px";
for(var i = sheArr.length - 1; i > 0; i--) {
sheArr[i].style.left = sheArr[i-1].style.left;
sheArr[i].style.top = sheArr[i-1].style.top;
}
}
function gor() {
for(var i = 0; i < gwArr.length; i++) {
if(she.offsetTop + she.offsetHeight >= gwArr[i].offsetTop && she.offsetTop <= gwArr[i].offsetTop + gwArr[i].offsetHeight && she.offsetLeft + she.offsetWidth >= gwArr[i].offsetLeft && she.offsetLeft <= gwArr[i].offsetLeft + gwArr[i].offsetWidth) {
console.log("撞上了");
document.body.removeChild(gwArr[i]);
creatShe();
gwArr.splice(i, 1);
if(gwArr.length < 1) {
clearInterval(timerId);
timerId = setInterval(creatDiv, 500);
}
}
}
}
function creatShe() {
var div = document.createElement("div");
div.style.background = "rgb(" + suiJi(0, 256) + "," + suiJi(0, 256) + "," + suiJi(0, 256) + ")";
div.style.width = "40px";
div.style.height = "40px";
div.style.position = "absolute";
div.style.borderRadius = "50%";
document.body.appendChild(div);
div.style.top = '-50px';
div.style.left = '-50px';
div.style.zIndex = m;
}
function suiJi(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
一个自己瞎写的鼠标跟随特效0-0
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 下面选了最近十年里,十位名人所做的毕业演讲。那么多的故事与经历,其实只想告诉你一件事: 面对迷茫和不确定的未来,我...