zoukankan      html  css  js  c++  java
  • 键盘移动小div(js原生)



    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>无标题文档</title>
    <style>
        div{
            100px;
            height:100px;
            background-color:red;
            position:absolute;
            left:100px;
            top:100px;
        }
    </style>
    </head>

        
    <body>
        <div id="div1"></div>
        <script>
            var div1 = document.getElementById("div1");
                var l = false,
                    t = false,
                    r = false,
                    b = false;
                
                document.onkeydown = function(ev) {
                    switch (ev.keyCode) {
                        case 37:
                            l = true;
                            break;
                        case 38:
                            t = true;
                            break;
                        case 39:
                            r = true;
                            break;
                        case 40:
                            b = true;
                            break;
                    }
                    
                    if (l) {
                        div1.style.left = div1.offsetLeft - 5 + "px";
                    }
                    if (t) {
                        div1.style.top = div1.offsetTop - 5 + "px";
                    }
                    if (r) {
                        div1.style.left = div1.offsetLeft + 5 + "px";
                    }
                    if (b) {
                        div1.style.top = div1.offsetTop + 5 + "px";
                    }
                                    
                }
                
                document.onkeyup = function() {
                    l = false;
                    t = false;
                    r = false;
                    b = false;
                }
        </script>
    </body>
    </html>

  • 相关阅读:
    react native android 应用状态(前端或后台)的判断
    react native native module
    Nodejs项目重复文件扫描
    clipboard.js文本复制到剪贴板的现代方法
    微信小程序如何跳转到另一个小程序
    百度小程序button去掉默认边框
    下拉框select中option居中样式
    css中如何实现左边的高度随着右边改变而改变
    js判断是否手机自动跳转移动端
    webpack安装整理
  • 原文地址:https://www.cnblogs.com/yuejie/p/5986357.html
Copyright © 2011-2022 走看看