簡單js製做推箱子

GIF動態效果:

 

 

代碼展現:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body onkeydown="change()">
<img src="1.jpg" id = "imgShow" style="width: 100px;
height: 100px;position: absolute;left:0px;top: 0px"/>
<script type="text/javascript">
    var img = document.getElementById("imgShow");
    img.left = 0;
    img.top = 0;
    alert(keyCode);
    function change(){
        //alert(keyCode);
        var keyCode = event.keyCode;
        if(keyCode == 37){
            img.left -= 70;

        }
        if(keyCode == 38){
            img.top -= 70;

        }
        if(keyCode == 39){
            img.left += 70;

        }
        if(keyCode == 40){
            img.top += 70;

        }
        img.style.left = img.left+"px",img.style.top = img.top+"px";
    }
</script>
</body>
</html>
相關文章
相關標籤/搜索