為什么我的代碼就是實現(xiàn)不了多物體運動?請大神看看。
<!doctype html>
<html>
<head>
??? <meta charset="UTF-8">
?? ?<title>Document</title>
<style type="text/css">
ul ,li{
?? ?list-style: none;
}
ul li{
width:200px;
height: 100px;
background: yellow;
margin-bottom: 20px;
}
</style>
<script type="text/javascript">
?? ?window.onload=function(){
??? var Ali=document.getElementsByTagName("li");
??? for(var i=0;i<Ali.length;i++){
?????? Ali[i].onmouseover=function(){
??????? startMove(this,400);
?????? }
?????? Ali[i].onmouseout=function(){
????? ??? ?startMove(this,200);
?????? }
??? }
?? ?}
var timer=null;
function startMove(obj,iTarget){
clearInterval(timer);
timer=setInterval(function(){
??? var speed=(iTarget-obj.offsetwidth)/8;
??? speed=speed>0?Math.ceil(speed):Math.floor(speed);
??? if(obj.offsetwidth=iTarget){
???? clearInterval(timer);
??? }
??? else{
?? ??? ?obj.style.width=obj.offsetwidth+speed+'px';
??? }
},30)
}
</script>
</head>
<body>
?? ?<ul>
?? ??? ?<li></li>
?? ??? ?<li></li>
?? ??? ?<li></li>
?? ?</ul>
</body>
</html>
2016-08-26
? ?var speed=(iTarget-obj.offsetwidth)/8;
為什么要除以8的
2016-08-09
不好意思重復了,已經(jīng)解決