怎么实现html元素跟随鼠标移动而移动?

怎么实现html元素跟随鼠标移动而移动?,第1张

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<style>

#div1 {width:100px height:100px background:red position:absolute}

</style>

<meta http-equiv="Content-Type" content="text/html charset=utf-8" />

<title>无标题文档</title>

<script>

document.onmousemove=function (ev)

{

    var oEvent=ev||event

    var oDiv=document.getElementById('div1')

    

    oDiv.style.left=oEvent.clientX+'px'

    oDiv.style.top=oEvent.clientY+'px'

}

</script>

</head>

<body>

<div id="div1">

</div>

</body>

</html>

mc1.onEnterFrame = function() {//mc1为影片剪辑图片的实例名

this._x+=(_xmouse-this._x)/5

this._y+=(_ymouse-this._y)/5

}

你随便弄个网页

以下代码插入<head>标签之内,保存预览,就可以看到很漂亮的文字环绕鼠标

<SCRIPT

LANGUAGE="JavaScript">

if

(document.all)

{

yourLogo

=

"我想有个家一个温暖的家"

//自己根据要求设置//

logoFont

=

"宋体"

logoColor

=

"#00ccff"

yourLogo

=

yourLogo.split('')

L

=

yourLogo.length

TrigSplit

=

360

/

L

Sz

=

new

Array()

logoWidth

=

100

logoHeight

=

-30

ypos

=

0

xpos

=

0

step

=

0.03

currStep

=

0

document.write('<div

id="outer"

style="position:absolutetop:0pxleft:0px"><div

style="position:relative">')

for

(i

=

0

i

<

L

i++)

{

document.write('<div

id="ie"

style="position:absolutetop:0pxleft:0px'

+'width:10pxheight:10pxfont-family:'+logoFont+'font-size:12px'

+'color:'+logoColor+'text-align:left">'+yourLogo[i]+'</div>')

}

document.write('</div></div>')

function

Mouse()

{

ypos

=

event.y

xpos

=

event.x

-

5

}

document.onmousemove=Mouse

function

animateLogo()

{

outer.style.pixelTop

=

document.body.scrollTop

for

(i

=

0

i

<

L

i++)

{

ie[i].style.top

=

ypos

+

logoHeight

*

Math.sin(currStep

+

i

*

TrigSplit

*

Math.PI

/

180)

ie[i].style.left

=

xpos

+

logoWidth

*

Math.cos(currStep

+

i

*

TrigSplit

*

Math.PI

/

180)

Sz[i]

=

ie[i].style.pixelTop

-

ypos

if

(Sz[i]

<

5)

Sz[i]

=

5

ie[i].style.fontSize

=

Sz[i]

/

1.7

}

currStep

-=

step

setTimeout('animateLogo()',

20)

}

window.onload

=

animateLogo

}

</script>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/6273855.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-19
下一篇 2023-03-19

发表评论

登录后才能评论

评论列表(0条)

保存