私はonmousedownを使って呼び出されるアニメーションを持っています。私はアニメーションを止める別の機能を持っています(onmouseup)。Javascript - ボタンが最初に押されたときのアニメーションのみ
問題は最初にしか動作しないことです。それは私がボタンを保持すると、それは動作し、押し下げを止めると停止しますが、最初の時間の後にどちらかのボタンを使用しようとすると何も起こりません。それは単に動きません。
これは私のHTMLコード(index.htmを)です:
<html>
<head><link rel='stylesheet' href='style.css'></head>
<body>
<img id='player' src='img/player.png' style='height:64px;'></img>
<div class='buttons'>
<button id='moveleft' onmousedown="OnButtonDownl (this)" onmouseup="OnButtonUpl (this)"><--</button>
<button id='moveright' onmousedown="OnButtonDownr (this)" onmouseup="OnButtonUpr (this)">--></button>
</div>
</body>
</html>
<script type='text/javascript' src='move.js'></script>
これは私のJavaScriptコード(move.js)です:
var elem = document.getElementById("player");
function OnButtonDownl (button) {
var posl = document.getElementById("player").style.left;
window.idl = setInterval(framel, 5);
function framel() {
posl--;
elem.style.left = posl + 'px';
}}
function OnButtonUpl (button) {
clearInterval(idl);
}
var elem = document.getElementById("player");
function OnButtonDownr (button) {
var posr = document.getElementById("player").style.left;
window.idr = setInterval(framer, 5);
function framer() {
posr++;
elem.style.left = posr + 'px';
}}
function OnButtonUpr (button) {
clearInterval(idr);
}
おそらく、ここで重要なのではなく、私のCSS(スタイルです.css):
body {
width: 100%;
height: 100%;
position:relative;
margin:0;
overflow:hidden;
}
#player {
position: absolute;
left:0;
bottom:0;
}
.buttons {
position:absolute;
right:0;
bottom:0;
}
ご協力いただきありがとうございます。
plunkrを追加できますか? – Baruch
2回目にボタンを押したときにコンソールに何かが表示されますか? –