美文网首页
JavaScript基础 键盘事件 与 onmousedown

JavaScript基础 键盘事件 与 onmousedown

作者: 0说 | 来源:发表于2018-03-05 11:48 被阅读0次

onmousedown鼠标按下
左中右键都会触发

document.onmousedown = function (){
    console.log( 1 );
}

e.which 返回左中右对应的数字 (不兼容IE8) 主流浏览器 左 1 中 2 右 3

document.onmousedown = function ( e ){
    e = e || window.event;
    console.log( e.which );
}

e.button 兼容IE8及以下 左 1 中 4 右 2

document.onmousedown = function ( e ){
    e = e || window.event;
    console.log( e.button );
}

兼容写法

function getButton(e){
        var evt = e || window.event;
        if(e){
            return e.which;
        }else{
            switch(evt.button){
                case 1:
                    return 1;
                case 4:
                    return 2;
                case 2:
                    return 3;
            }
        }
    }

键盘事件

能获得焦点的元素才有键盘事件 document能获得焦点 div就不能获得

onkeydown 键盘按下时触发 任意键都能触发
onkeypress 键盘按下时触发 只有能键入内容的按键和enter才能触发 (shift、ctrl不能)
onkeyup 键盘抬起时触发

document.onkeydown = function (){
    console.log( 111 );//一直按着就一直触发
}
document.onkeyup = function (){
    console.log( 222 ); 
}

键值e.keyCode

<body>
<input id="inp" type="text">
<script>
    var oInp = document.getElementById( 'inp' );
    oInp.onkeydown = function (e){
        e = e || window.event;
        console.log( e.keyCode )
    }
</script>
Animation.gif

案例

<style>
        *{margin:0;padding:0;font-family: Microsoft YaHei,serif;}
        li{list-style: none;}
        #box{
            position: absolute;
            top: 200px;
            left: 200px;
            width: 100px;
            height: 100px;
            background: #f60;
        }
    </style>
</head>
<body>
<div id="box"></div>
<script>
    var box = document.getElementById( 'box' ),
        Left = box.offsetTop,
        Top = box.offsetLeft,
        speed = 5;
    document.onkeydown = function (e){
        e = e || window.event;
        var keyVal = e.keyCode;
        switch ( keyVal ){
            case 37: //向左
                Left -= speed;
                break;
            case 38: //向上
                Top -= speed;
                break;
            case 39: //向右
                Left += speed;
                break;
            case 40: //向下
                Top += speed;
                break;
        }
        box.style.left = Left + 'px';
        box.style.top = Top + 'px';
        console.log( e.keyCode )
    }
//    document.onkeyup = function (){
//        console.log( '222' )
//    }

</script>
Animation.gif

斜着走

<script>
    var box = document.getElementById( 'box' ),
        Left = box.offsetTop,
        Top = box.offsetLeft,
        speed = 1;
    document.onkeydown = function (e){
        e = e || window.event;
        var keyVal = e.keyCode;
        switch ( keyVal ){
            case 37: //向左
                if( box.m37 === undefined ) {
                    box.m37 = setInterval(function () {//按下时没有抬起时就一直开启定时器, 
                         //当按另一个键时另一个键的定时器也会开启 就会同时移动
                        Left -= speed;
                        box.style.left = Left + 'px';
                    }, 10);
                }
                break;
            case 38: //向上
                if( box.m38 === undefined ) {
                    box.m38 = setInterval(function () {
                        Top -= speed;
                        box.style.top = Top + 'px';
                    }, 10);
                }
                break;
            case 39: //向右
                if( box.m39 === undefined ) {
                    box.m39 = setInterval(function () {
                        Left += speed;
                        box.style.left = Left + 'px';
                    }, 10);
                }
                break;
            case 40: //向下
                if( box.m40 === undefined ) {
                    box.m40 = setInterval(function () {
                        Top += speed;
                        box.style.top = Top + 'px';
                    }, 10);
                }
                break;
        }
    }

    document.onkeyup = function ( e ){
        e = e || window.event;
        switch ( e.keyCode ){

            case 37:
                clearInterval( box.m37 );
                box.m37 = undefined;
                console.log( box.m37 )
                break;
            case 38:
                clearInterval( box.m38 );
                box.m38 = undefined;
                break;
            case 39:
                clearInterval( box.m39 );
                box.m39 = undefined;
                break;
            case 40:
                clearInterval( box.m40 );
                box.m40 = undefined;
                break;
        }
    }

</script>
Animation.gif

相关文章

  • JavaScript基础 键盘事件 与 onmousedown

    onmousedown鼠标按下左中右键都会触发 e.which 返回左中右对应的数字 (不兼容IE8) 主流浏览器...

  • DOM事件

    DOM事件的概念 事件是javascript和HTML交互基础,。交互;比如鼠标点击事件、敲击键盘事件等。这样的事...

  • JS事件

    鼠标事件 onmousedown:鼠标按钮被按下(左键或者右键)时触发。不能通过键盘触发。onmouseup:鼠标...

  • JavaScript ☞ day3

    JavaScript基础学习笔记之JavaScript进阶 焦点事件 鼠标事件-单击与双击 鼠标事件-mouseo...

  • JavaScript鼠标事件及event坐标

    一、事件 1、onclick 事件会在对象被点击时发生请注意: onclick 与 onmousedown 不同。...

  • JavaScript键盘事件

    键盘事件与keyCode属性onkeydown:在用户按下一个键盘按键时发生onkeypress:在按下键盘按键时...

  • 事件

    事件触发方法:onclick="单击触发事件";ondblclick="双击触发事件";onmousedown="...

  • JavaScript基础与DOM

    JavaScript基础与DOM 键盘按下与松开 图片轮播 xml Dom 节点信息: 每个节点都有包含关于节点某...

  • JavaScript事件

    事件是可以被 JavaScript 侦测到的行为。 事件类型 鼠标事件 键盘事件 其他事件[https://www...

  • 事件

    鼠标事件 onmousedown, onmouseup, onclick, ondbclick, onmousew...

网友评论

      本文标题:JavaScript基础 键盘事件 与 onmousedown

      本文链接:https://www.haomeiwen.com/subject/xewqfftx.html