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>
到页面右边后只向左反向行驶一次,然后向右,如此循环,我知道是逻辑上的问题,具体就是值范围的判断逻辑不正确,可一时又没想出正确的逻辑。
补充:不要用跑马灯,我有别的用途,比如做游戏。
答案:代码比较长,首先你的setInterval()用错了,是setInterval(move,1000);
其他:怎么不用跑马灯标签呢? <marquee>
上一个:请帮我解释下下面的javascript代码 谢谢了!!!求解释清楚一点!!!
下一个:javascript这是为什么啊