window.onload=function(){ document.body.onkeyup=function(evt){ var step=20; var box=document.getElementById("box"); var img=document.getElementById("img"); var left = parseInt(img.style.left); /*获得图片左边距的数值*/ var top = parseInt(img.style.top); /*获得图片高的数值*/ var d_width = box.offsetWidth; var d_height = box.offsetHeight; /*获得box控件的的绝对(宽度,高度)*/ var b_width = img.width; var b_height = img.height; /*获得图片的(宽度,高度)*/ var x_max = d_width-b_width-step; var y_max = d_height-b_height-step; /**/ var e = evt ? evt: window.event; if(e.keyCode == 37 && left >=step){ img.style.left = left-step+"px"; } if(e.keyCode == 38 && top >=step){ img.style.top = top-step+"px"; } if(e.keyCode == 39 && left<= x_max){ img.style.left = left+step+"px"; } if(e.keyCode == 40 && top <=y_max){ img.style.top = top+step+"px"; } }}
添加回答
举报
0/150
提交
取消