zoukankan      html  css  js  c++  java
  • asdw键移动图片.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
    *{padding: 0;margin: 0;}
    #box{
    200px;height: 200px;position: absolute;top: 100px;left: 100px;
    }
    </style>
    </head>
    <body>

    <div id="box">
    <img src="img/Game figure.jpg"/>
    </div>
    <script type="text/javascript">
    //ASCII码 上87下83左65右68
    var o=document.getElementById('box');
    document.onkeydown=function (e)
    {
    e=e||window.event;
    //console.log(e.keyCode);//得到上下左右四个按键码
    //判断按键
    if (e.keyCode==87)
    {
    var preTop=window.getComputedStyle(o,null).top;//上
    preTop=parseInt(preTop);//去单位
    //在原来图片的位置减去10px
    o.style.top=preTop-10+'px';
    }else if (e.keyCode==83)
    {
    var preTop=window.getComputedStyle(o,null).top;//下
    preTop=parseInt(preTop);
    o.style.top=preTop+10+'px';
    }else if (e.keyCode==65)
    {
    var preLeft=window.getComputedStyle(o,null).left;//左
    preLeft=parseInt(preLeft);
    o.style.left=preLeft-10+'px';
    }else if (e.keyCode==68)
    {
    var preLeft=window.getComputedStyle(o,null).left;//右
    preLeft=parseInt(preLeft);
    o.style.left=preLeft+10+'px';
    }else
    {
    alert('按键点击错误,请点击有效按键!');
    }
    }
    </script>

    </body>
    </html>

  • 相关阅读:
    软件工程第一次作业
    软工热身作业
    OO第四单元
    面向对象——规格总结
    OO电梯作业总结
    JAVA实现表达式求导运算的分析总结
    提问回顾与个人总结
    Flutter的环境配置以及一些常见问题
    软件案例分析
    软件工程结对作业
  • 原文地址:https://www.cnblogs.com/d534/p/12691882.html
Copyright © 2011-2022 走看看