2012-03-12 114 views
0

我试图从右侧移到一个img左的Javascript搬东西

但是代码只从左至右

这部作品是左到右一个这一个工程

为例
<script language="javascript"> 
var x = 310; //Starting Location - left 

var dest_x = 300; //Ending Location - left 

var interval = 2; //Move 10px every initialization 

function moveImage() { 
//Keep on moving the image till the target is achieved 
if(x<dest_x) x = x + interval; 


//Move the image to the new location 

document.getElementById("ufo").style.left = x+'px'; 

if ((x+interval < dest_x)) { 
    //Keep on calling this function every 100 microsecond 
    // till the target location is reached 
    window.setTimeout('moveImage()',10); 
} 

有人知道这个问题吗?真的很感激它!

+0

尝试使间隔为负值,或将其从x中不加入。你现在开始在310和每个循环加2,所以你不会下降到300 – Dampsquid 2012-03-12 23:21:17

回答

0

我想象这个问题源于使用document.getElementById("ufo").style.left如果你是从右向左移动你有没有试过将这个值改为document.getElementById("ufo").style.right

0

左侧属性定义了距左侧的距离。您正在增加左侧值,所以自然会导致它从左侧移动到右侧。您可以通过减少属性从右向左移动。基本上,只需将间隔设置为-2。另外,如果(x < dest_x)变为if(x> dest_y),则应该更改,否则它将永远不会运行。

穿过它: 从左侧310像素开始,并将每次迭代的行程距离设置为+2。 您检查312是否小于300(您的目的地);不是这样。 如果是,那么你可以将x值更改为310 + 2 = 312;你现在将从左侧312像素。 然后在重复循环之前检查x +距离(312)是否小于300。

+0

我试着你说,但它仍然没有移动:( var x = 310; //开始位置 - 顶部 var dest_x = 300 ; //结束位置 - 顶部 变种间隔= -2; //移动2px的每个初始化 功能moveMenu(){ \t //保持在图像移动,直到目标实现 \t如果(X> dest_y)X = x + interval; \t \t //将图片移动到新位置 \t document.getElementById(“menu”)。style.top = x +'px'; \t如果((X +间隔 2012-03-12 23:49:38

+0

对不起,我迟了一点回复,在做出这个回复后不得不去做点事情。如果((x + interval dest_x) – Treip 2012-03-13 03:58:21