JS實現跟隨鼠標的魔法文字


JS學習資料時,看到的有趣的代碼,文字隨着鼠標的移動而移動!貼一下源碼,分享一下!

<html> <head> <title> 跟隨鼠標的魔法文字</title> <SCRIPT language=JavaScript1.2>

var msg='看的見我的漂移嗎?';

var msgColor='000000'

var dismissafter=0

var amount=5,ypos=0,xpos=0,Ay=0,Ax=0,By=0,Bx=0,Cy=0,Cx=0,Dy=0,Dx=0,Ey=0,Ex=0;

 if (document.all){ //IE下——輸出層

    document.write("<div id='outer' style='position:absolute;top:0px;left: 0px'>");

    document.write("<div id='inner' style='position:relative'>");

    for (i = 0; i < amount; i++)

    {document.write('<div id="text"'+i+' style="position:absolute;top:0px;left: 0px;font-family:Courier New;font-size: 16px;color:'+msgColor+'">'+msg+'</div>')}

    document.write("</div>");

    document.write("</div>");

    function iemouse()                    //通過鼠標事件,獲取鼠標的坐標

    {ypos = document.body.scrollTop + event.y;xpos = document.body.scrollLeft + event. x;mouseFollow()}

}

function mouseFollow(){

  if (document.all){//IE下,設置層的位置

    outer.all.inner.all[0].style.pixelTop=ay;outer.all.inner.all[0].style. pixelLeft =ax;

    outer.all.inner.all[1].style.pixelTop=by;outer.all.inner.all[1].style. pixelLeft =bx;

    outer.all.inner.all[2].style.pixelTop=cy;outer.all.inner.all[2].style. pixelLeft =cx;

    outer.all.inner.all[3].style.pixelTop=Dy;outer.all.inner.all[3].style. pixelLeft =Dx;

    outer.all.inner.all[4].style.pixelTop=Ey;outer.all.inner.all[4].style. pixelLeft =Ex;

}

}

function move(){

    if (dismissafter!=0)

    setTimeout("hideMove()",dismissafter*1000)

    if (document.all){window.document.onmousemove = iemouse}    //綁定鼠標事件

    ey = Math.round(Ey+=((ypos+20)-Ey)*2/2);ex = Math.round(Ex+=((xpos+20) -Ex)*2/2);

    dy = Math.round(Dy+=(ey - Dy)*2/4);dx = Math.round(Dx+=(ex - Dx)*2/4);     cy = Math.round(Cy+=(dy - Cy)*2/6);cx = Math.round(Cx+=(dx - Cx)*2/6);

    by = Math.round(By+=(cy - By)*2/8);bx = Math.round(Bx+=(cx - Bx)*2/8);

    ay = Math.round(Ay+= (by - Ay)*2/10);ax = Math.round(Ax+= (bx - Ax)*2/10);

    mouseFollow();

    jumpstart=setTimeout('move()',10);                             //定時執行捕獲操作

}

function hideMove(){

    if (document.all){   //IE瀏覽器的情況下

        for (i2=0;i2<amount;i2++){

            outer.all.inner.all[i2].style.visibility="hidden"      //設置為隱藏

            clearTimeout(jumpstart)                               //清除定時器

            }     } }

window.onload=move;                                  //窗體一加載便觸發飄移事件

</SCRIPT>
</head>
<body>
</body>
</html>

提示: 鼠標周圍可以跟隨圖片、文字和星星等。本例將提供一些類似對聯的文字,一直跟隨鼠標移動。 重點是獲取鼠標的活動坐標,根據坐標動態設置多個層,多個層的交替形成魔法效果。“event.x”和“event.y”分別獲取鼠標的x和y坐標,然后使用“document.onmousemove= iemouse”綁定鼠標的移動事件。

有個小小問題想請教一下大牛:如下代碼:

<html>
  <head>
  <title>彈出框問題</title>

  <SCRIPT type="text/javascript">  

  setInterval("alert('你說你老是往外蹦,真頭疼!')",100);

  </SCRIPT>

  </head>

  <body>
  </body>
</html>
如果我設置了一個時鍾,打開頁面即執行,由於設置的時鍾的時間間隔很短,總是彈出提示框,以至於無法打開其他的網頁,如何處理?


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM