<div id = "box"></div>
<script>
console.log(window.innerHeight)
var box = document.querySelector('#box')
var maxTop = window.innerHeight - box.offsetHeight
var maxLeft = window.innerWidth - box.offsetWidth
document.onkeydown = function(e){
e = e || window.event
var code = e.keyCode || e.which
console.log(code)
switch (code){
case 87:
var top = box.offsetTop - 10
if(top < 0) top = 0
box.style.top = top +'px'
break;
case 32:
var top = box.offsetTop + 10
if(top < 0) top = 0
box.style.top = top +'px'
break;}}
</script>
</body>