JS特效代码示例:跟随鼠标的方块

2012-06-19 00:42:58  来源:网页教学网 

网页制作Webjx文章简介:跟随鼠标的方块。

<html xmlns="http://www.webjx.com/1999/xhtml">
<head>
<title>跟随鼠标的方块</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<!--把下面代码加到<head>与</head>之间-->
<style type="text/css">
#a{width:15px;height:15px;font-size:0px;border:solid 1px red;background-color:red;position:absolute;border:1px solid #000;}
</style>
</head>
<body>
<!--把下面代码加到<body>与</body>之间-->
<div id="a"></div>
<script type="text/javascript">
window.onload=function(){
var d=document;
d.attachEvent?
d.attachEvent("onmousemove",bb)
:
d.addEventListener("mousemove",bb,false)
}
function bb(oEvent){
var t=document.getElementById("a");
var o=oEvent||window.event;
t.style.left=o.clientX-t.offsetWidth/2+'px';
t.style.top=o.clientY-t.offsetHeight/2+'px';
}
</script>
</body>
</html>
更多