<div style="width: 100px;height: 100px;background-color: brown;opacity: 0.8;position: absolute;top: 0;left: 0;"></div>
<script type="text/javascript">
var div = document.getElementsByTagName("div")[0];
//设置定时器解决转换方向时卡顿的问题
var speed = 20;
var dir = 0
setInterval(function(){
switch(dir){
case 37:
div.style.left = div.offsetLeft - speed +"px";
break;
case 39:
div.style.left = div.offsetLeft + speed + "px";
break;
case 38:
div.style.top = div.offsetTop - speed + "px";
break;
case 40:
div.style.top = div.offsetTop + speed + "px";
break;
}
},30);
document.onkeydown = function(event){
var event = event || window.event;
dir = event.keyCode;
if(event.ctrlKey){
speed = 50;
}
};
document.onkeyup = function(){
dir = 0;
speed = 10;
}
</script>
11-10
1556
08-20
483
05-25
1023