JS特效代码示例:跟随鼠标的方块
http://www.itjxue.com 2015-08-06 23:00 来源:未知 点击次数:
<html xmlns="http://www.itjxue.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>