<script> ? ?window.onload = function () { ? ? ? ?var odiv = document.getElementById("div1"); ? ? ? ?var timer = null; ? ? ? ?document.onkeyup = function () { ? ? ? ? ? ?clearInterval(timer); ? ? ? ? ? ?timer=null; ? ? ? ?} ? ? ? ?document.onkeydown = function (ev) { ? ? ? ? ? ?var oEven = ev || event; ? ? ? ? ? ?clearInterval(timer); ? ? ? ? ? ? ? ?timer = setInterval(function () { ? ? ? ? ? ? ? ? ? ?if (oEven.keyCode == 37) { ? ? ? ? ? ? ? ? ? ? ? ?odiv.style.left = odiv.offsetLeft - 10 + "px"; ? ? ? ? ? ? ? ? ? ?} else if (oEven.keyCode == 39) { ? ? ? ? ? ? ? ? ? ? ? ?odiv.style.left = odiv.offsetLeft + 10 + "px"; ? ? ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ?}, 100) ? ? ? ?} ? ?}</script>
下面這段代碼按下右鍵之后只是位移了一小段就停止了,如果修改了定時器的間隔時間,又能一直動下去,求解。
慕田峪8701529
2017-08-21 21:54:54