Javascript教程:跟随鼠标的方块代码
<html xmlns="">
<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>
代码给大家介绍到这里了,赶紧试一试吧!