【发布时间】:2016-07-12 15:46:47
【问题描述】:
我有一个使用 onmousedown 调用的动画。我有另一个停止动画的功能(onmouseup)。
问题是它只能在第一次工作。我的意思是当我按住按钮时,它会起作用,然后在我停止按下时停止,但是如果我在第一次之后尝试使用其中任何一个按钮,则什么也没有发生。它只是不会移动。
这是我的 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 (style.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吗?
-
第二次尝试按下按钮时控制台是否显示任何内容?
标签: javascript html css animation