困難

2016-05-12 7 views
2

to move red box over yellow container - onmouseover-> move right , onmouseout-> move left困難

は、Iはpos++pos--を使用して移動しようとしました。しかしそれはうまくいかなかった。助けてください。

ここにコードがあります。

var id_l, id_r; 
 
function right() { 
 
\t clearInterval(id_l); 
 
    var box = document.getElementById("animate"); 
 
\t var pos=box.style.left; 
 
\t id_r=setInterval(move,5); 
 
\t function move() { 
 
\t \t if(pos==900) { 
 
\t \t \t clearInterval(id_r); 
 
\t \t } 
 
\t \t else { 
 
\t \t  pos++; 
 
\t \t  box.style.left = pos + "px"; 
 
\t  } 
 
\t } 
 
} 
 
function left() { 
 
\t clearInterval(id_r); 
 
    var box = document.getElementById("animate"); 
 
\t var pos=box.style.left; 
 
\t id_l=setInterval(move,5); 
 
\t function move() { 
 
\t \t if(pos==0) { 
 
\t \t \t clearInterval(id_l); 
 
\t \t } 
 
\t \t else { 
 
\t \t  pos--; 
 
\t \t  box.style.left = pos + "px"; 
 
\t  } 
 
\t } 
 
}
#container { 
 
    position: relative; 
 
    width: 1000px; 
 
    height: 100px; 
 
\t background-color: yellow; 
 
} 
 
#animate { 
 
    position: absolute; 
 
    width: 100px; 
 
    height: 100px; 
 
    background-color: red; 
 
\t left: "200px"; 
 
}
<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
\t  <link rel="stylesheet" href="styles/styles.css"> \t \t 
 
\t </head> 
 
\t <body> 
 
\t  <div id="container" onmouseover="right()" onmouseout="left()"> 
 
\t \t  <div id="animate"> 
 
\t \t \t </div> 
 
\t \t </div> 
 
\t \t <script src="scripts/javascript.js"></script> 
 
\t </body> 
 
</html>

答えて

1

あなたはそれが間違ってやっていました。あなたが位置を取得すると、それは、などのフォーマット100pxであなたを与えるあなたはpxをトリミングする必要があり、その後、あなたの計算を行う

var id_l, id_r; 
 
function right() { 
 
\t clearInterval(id_l); 
 
    var box = document.getElementById("animate"); 
 
\t var pos=box.style.left; 
 
    pos = pos.toString().substr(0, pos.length-2); // do not read `px` 
 
\t id_r=setInterval(move,5); 
 
\t function move() { 
 
\t \t if(pos==900) { 
 
\t \t \t clearInterval(id_r); 
 
\t \t } 
 
\t \t else { 
 
\t \t  pos++; 
 
\t \t  box.style.left = pos + "px"; 
 
\t  } 
 
\t } 
 
} 
 
function left() { 
 
    
 
\t clearInterval(id_r); 
 
    var box = document.getElementById("animate"); 
 
\t var pos=box.style.left; 
 
    pos = pos.toString().substr(0, pos.length-2); // do not read px 
 
    
 
\t id_l=setInterval(left,5); 
 
\t function left() { 
 
     
 
      
 
     
 
\t \t if(pos==0) { 
 
     
 
\t \t \t clearInterval(id_l); 
 
\t \t } 
 
\t \t else { 
 
\t \t  pos--; 
 
\t \t  box.style.left = pos+"px"; 
 
\t  } 
 
\t } 
 
}
#container { 
 
    position: relative; 
 
    width: 1000px; 
 
    height: 100px; 
 
\t background-color: yellow; 
 
} 
 
#animate { 
 
    position: absolute; 
 
    width: 100px; 
 
    height: 100px; 
 
    background-color: red; 
 
\t left: "200px"; 
 
}
<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
\t  <link rel="stylesheet" href="styles/styles.css"> \t \t 
 
\t </head> 
 
\t <body> 
 
\t  <div id="container" onmouseover="right()" onmouseout="left()"> 
 
\t \t  <div id="animate"> 
 
\t \t \t </div> 
 
\t \t </div> 
 
\t \t <script src="scripts/javascript.js"></script> 
 
\t </body> 
 
</html>

0

それはGETですので、あなたは、両方のmove()parseIntを追加することにより、正しい行うことができます文字列!

element.style.leftはインラインスタイルでのみ機能します!だからgetComputedStyleが良いです