当前位置:编程学习 > JS >>

javascript控制图片移动到页面右边再返向行驶的代码,出了问题,代码如下

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <style> body{ text-align: center; margin-top:200px; } #plane{ position: absolute; } </style> </head> <body> <img src='../images/plane.png' id='plane' /> <script> var obj=document.getElementById('plane'); var pos_left=document.body.scrollLeft; var pos_right=document.body.scrollWidth; function moveRight(){ obj.style.left=obj.offsetLeft+100+'px'; } function moveLeft(){ obj.style.left=obj.offsetLeft-100+'px'; } function move(){ if(obj.offsetLeft<pos_right){ moveRight(); }else if(obj.offsetLeft>=pos_right){ moveLeft(); } } setInterval('move()', 1000); </script> </body> </html> 到页面右边后只向左反向行驶一次,然后向右,如此循环,我知道是逻辑上的问题,具体就是值范围的判断逻辑不正确,可一时又没想出正确的逻辑。
补充:不要用跑马灯,我有别的用途,比如做游戏。
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,