0
申し訳ありません、私はJavaScriptプログラミングの新機能です。私は別の方向ボタンをクリックすると対応する方向に移動するインタラクティブなdivを作成しようとしていました。ここにコードがあります。divを4方向に移動
reset();
function reset() {
\t var d = document.getElementById('animate');
\t d.style.position = "absolute";
\t d.style.left = 50+'px';
\t d.style.top = 50+'px';
}
function moveRight() {
\t var elem = document.getElementById("animate");
\t var rect = elem.getBoundingClientRect();
\t var pos = rect.left;
\t var id = setInterval(frameRight, 500);
\t function frameRight() {
\t \t pos = pos + 10;
\t elem.style.left = pos + 'px';
\t clearInterval(id);
}
}
function moveLeft() {
\t var elem = document.getElementById("animate");
\t var rect = elem.getBoundingClientRect();
\t var pos = rect.left;
\t var id = setInterval(frameLeft, 500);
\t function frameLeft() {
\t \t pos = pos - 10;
\t elem.style.left = pos + 'px';
\t clearInterval(id);
}
}
function moveUp() {
\t var elem = document.getElementById("animate");
\t var rect = elem.getBoundingClientRect();
\t var pos = rect.top;
\t var id = setInterval(frameTop, 500);
\t function frameTop() {
\t \t pos = pos - 10;
\t elem.style.top = pos + 'px';
\t clearInterval(id);
}
}
function moveDown() {
\t var elem = document.getElementById("animate");
\t var rect = elem.getBoundingClientRect();
\t var pos = rect.top;
\t var id = setInterval(frameDown, 500);
\t function frameDown() {
\t \t pos = pos + 10;
\t elem.style.top = pos + 'px';
\t clearInterval(id);
}
}
#container {
width: 400px;
height: 400px;
position: relative;
background: yellow;
}
#animate {
width: 50px;
height: 50px;
background-color: red;
}
<p>
<button onclick="moveRight()">Right</button><button onclick="moveLeft()">Left</button><button onclick="moveUp()">Up</button><button onclick="moveDown()">Down</button><button onclick="reset()">Reset</button>
</p>
<div id ="container">
<div id ="animate"></div>
</div>
問題は、私は右のボタンを押すのですwheneそれは完璧に動くが、同じdosen'tは、左ボタンに起こるということです。上下のボタンのためだけにそれは下に行く。私はちょうど各方向に20ピクセルの動きが欲しい。前もって感謝します。
ような短い時間で、このソリューションのおかげでアンドレイを。私にとっては完璧です。 – suchit