8.7w+
社区成员
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>float</title>
<style type="text/css">
<!--
#apDiv1 {
position:absolute;
left:100px;
top:50px;
width:150px;
height:150px;
z-index:1;
background-color: #0000FF;
}
-->
</style>
<script language="javascript">
var x=2;
function move(){
if(document.getElementById("apDiv1").style.pixelLeft<0 ||
document.getElementById("apDiv1").style.pixelLeft > document.body.offsetWidth - 200 ){
x *= (-1);
}
document.getElementById("apDiv1").style.pixelLeft += x;
window.setTimeout("move()", 10);
}
</script>
</head>
<body onLoad="move()">
<div id="apDiv1"></div>
</body>
</html>
全局变量 整个函数 都共享这一个变量x
局部变量 每次调用move函数 都重新给赋值为x = 2当然就不对了