<style>
*{margin: 0;padding: 0}
</style>
</head>
<body>
<div id="box"></div>
<script>
var box=document.getElementById("box");
window.onkeydown=function (e) {
if(e.keyCode==39){
var L=box.offsetLeft+10
box.style.marginLeft=L+"px"
}
if(e.keyCode==37){
var L=box.offsetLeft-10
box.style.marginLeft=L+"px"
}
if(e.keyCode==38){
var T=box.offsetTop-10
box.style.marginTop=T+"px"
}
if(e.keyCode==40){
var B=box.offsetTop+10
box.style.marginTop=B+"px"
}
};
function yidong(id) {
var box=document.getElementById(id)
box.onmousedown=function (ev) {
var w=ev.clientX-box.offsetLeft;
var h=ev.clientY-box.offsetTop;
window.onmousemove=function (e) {
var L=e.clientX-w;
var T=e.clientY-h;
box.style.left=L+"px";
box.style.top=T+"PX"
};
box.onmouseup=function () {
window.onmousemove=null
}
}
}
yidong("box")
</script>