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.οnlοad=move;                                  //窗体一加载便触发飘移事件

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

提示: 鼠标周围可以跟随图片、文字和星星等。本例将提供一些类似对联的文字,一直跟随鼠标移动。 重点是获取鼠标的活动坐标,根据坐标动态设置多个层,多个层的交替形成魔法效果。“event.x”和“event.y”分别获取鼠标的x和y坐标,然后使用“document.οnmοusemοve= iemouse”绑定鼠标的移动事件。

有个小小问题想请教一下大牛:如下代码:

<html>
  <head>
  <title>弹出框问题</title>

  <SCRIPT type="text/javascript">  

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

  </SCRIPT>

  </head>

  <body>
  </body>
</html>
如果我设置了一个时钟,打开页面即执行,由于设置的时钟的时间间隔很短,总是弹出提示框,以至于无法打开其他的网页,如何处理?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的js代码实现跟随鼠标粒子效果: HTML: ```html <canvas id="canvas"></canvas> ``` CSS: ```css #canvas { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } ``` JS: ```javascript var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var particles = []; var mouse = { x: undefined, y: undefined } window.addEventListener("mousemove", function(event) { mouse.x = event.x; mouse.y = event.y; }); function Particle(x, y, radius, color) { this.x = x; this.y = y; this.radius = radius; this.color = color; this.radians = Math.random() * Math.PI * 2; this.velocity = 0.05; this.distanceFromCenter = Math.random() * 50 + 100; this.lastMouse = {x: x, y: y}; this.draw = function(lastPoint) { ctx.beginPath(); ctx.strokeStyle = this.color; ctx.lineWidth = this.radius; ctx.moveTo(lastPoint.x, lastPoint.y); ctx.lineTo(this.x, this.y); ctx.stroke(); ctx.closePath(); } this.update = function() { // 记录上一帧的鼠标位置 this.lastMouse.x += (mouse.x - this.lastMouse.x) * 0.05; this.lastMouse.y += (mouse.y - this.lastMouse.y) * 0.05; // 运动轨迹 this.radians += this.velocity; this.x = this.lastMouse.x + Math.cos(this.radians) * this.distanceFromCenter; this.y = this.lastMouse.y + Math.sin(this.radians) * this.distanceFromCenter; this.draw(this.lastMouse); } } function init() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; for (var i = 0; i < 50; i++) { var radius = Math.random() * 3 + 1; var color = "rgba(255, 255, 255, 0.5)"; particles.push(new Particle(canvas.width / 2, canvas.height / 2, radius, color)); } } function animate() { requestAnimationFrame(animate); ctx.fillStyle = "rgba(0, 0, 0, 0.1)"; ctx.fillRect(0, 0, canvas.width, canvas.height); for (var i = 0; i < particles.length; i++) { particles[i].update(); } } init(); animate(); ``` 这段代码创建了一个画布,并且在鼠标移动时跟随鼠标移动的粒子效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值