zoukankan      html  css  js  c++  java
  • 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>
    如果我设置了一个时钟,打开页面即执行,由于设置的时钟的时间间隔很短,总是弹出提示框,以至于无法打开其他的网页,如何处理?

  • 相关阅读:
    .NET 实现复制粘贴功能
    调用接口并且判断是否写日志(用一个参数来控制)
    Javascript知识——事件
    Swift开发的几个小技巧(整理自图拉鼎的博客)
    Swift 闭包表达式
    使用Xcode 的Debugging
    iOS开发中的ARC内存管理de技术要点
    iOS客户端de公共WIFI解决方案
    Crash log符号化与调试信息
    iOS NSNotificationCenter 使用姿势详解
  • 原文地址:https://www.cnblogs.com/SanMaoSpace/p/2358513.html
Copyright © 2011-2022 走看看