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;
//創(chuàng)建隨機的食物
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
最后編輯于 :
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門份殿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來膜钓,“玉大人,你說我怎么就攤上這事卿嘲∷绦保” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵拾枣,是天一觀的道長沃疮。 經(jīng)常有香客問我,道長梅肤,這世上最難降的妖魔是什么司蔬? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮姨蝴,結果婚禮上俊啼,老公的妹妹穿的比我還像新娘。我一直安慰自己左医,他們只是感情好授帕,可當我...
- 文/花漫 我一把揭開白布同木。 她就那樣靜靜地躺著,像睡著了一般跛十。 火紅的嫁衣襯著肌膚如雪彤路。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼姆吭,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了唁盏?” 一聲冷哼從身側響起内狸,我...
- 正文 年R本政府宣布崎弃,位于F島的核電站甘晤,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏饲做。R本人自食惡果不足惜线婚,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望盆均。 院中可真熱鬧酌伊,春花似錦、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至奏候,卻和暖如春循集,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蔗草。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 下面選了最近十年里,十位名人所做的畢業(yè)演講替蛉。那么多的故事與經(jīng)歷贯溅,其實只想告訴你一件事: 面對迷茫和不確定的未來,我...