感觉很流畅的键盘控制(带惯性) ask var keyCache = []; var isCache = false; var cacheNum = 0; document.onkeydown = insertKey; document.onkeyup = function(){if(!isCache && event.keyCode>=37&& event.keyCode<=40) keyCache.length = 0;} function insertKey(){ var kc = event.keyCode; if(kc>=37 && kc<=40){ if(kc!=keyCache[keyCache.length-1]){ keyCache.length = 0; keyCache.push(kc,kc,kc,kc,kc,kc) //保证流畅多注入6个 isCache = true; } keyCache.push(kc); } } window.setInterval("writeCache()", 50); function writeCache(){ if(keyCache.length==0) return; var kc = keyCache.shift(); switch(kc){ case 37: man.style.left = parseInt(man.style.left) - 5;break; case 38: man.style.top = parseInt(man.style.top) - 5;break; case 39: man.style.left = parseInt(man.style.left) + 5;break; case 40: man.style.top = parseInt(man.style.top) + 5;break; } if(isCache) cacheNum++; if(cacheNum == 5){ isCache = false; cacheNum = 0; } } [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]