一旦在用户浏览器中禁用箭头键滚动

一旦在用户浏览器中禁用箭头键滚动

本文介绍了一旦在用户浏览器中禁用箭头键滚动,如何启用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户浏览器中禁用箭头键滚动Zeta已获得有关如何禁用箭头键的正确答案.

Disable arrow key scrolling in users browserZeta was given correct answer for how to disable the arrow keys.

禁用后,我需要重新启用箭头键导航.

I need to re-enable arrow key navigation once its disabled.

$('main navigation').has('drop down').bind('keyup', function (e) {
    if (e.keyCode == 40) {hover(e,$(this));} //Down Arrow Key press, how mega menu
    if (e.keyCode == 38) {unhover(e);} //Up Arrow Key Press, hide mega menu
    if (e.keyCode == 9) {checkTab();} //check if tab pressed
}

var checkTab = function (){
        // i check, if tab focus to the main menu anchor
    if($('main navigation').is(':focus')){
        var keys = [];
        window.addEventListener("keydown",
            function(e){
                keys[e.keyCode] = true;
                switch(e.keyCode){
                    case 37: case 39: case 38:  case 40: // Arrow keys
                    case 32: e.preventDefault(); break; // Space
                    default: break; // do not block other keys
                }
            },
        false);
        window.addEventListener('keyup',
            function(e){
                keys[e.keyCode] = false;
            },
        false);
        console.log('focused')
    }
    else {
        console.log('f out')
    }
}

这工作正常,但是由于我要禁用window.event绑定.一旦禁用,我无法启用.在这一点上需要帮助.

this works fine, but as I have bind window.event to disable. once disabled I cant enable. Need help in this point.

推荐答案

使用另一个变量来确定您当前是否应该锁定箭头键:

Use another variable to determine whether you should currently block the arrow keys:

var useArrowKeysToScroll = false;

/* ... */
function(e){
    keys[e.keyCode] = true;
    if(!useArrowKeysToScroll &&
           ((e.keyCode >= 37 && e.keyCode <= 40) ||
             e.keyCode == 32)){
        e.preventDefault();
    }
    /* handle other keys etc... */
}, /* ... */

如果useArrowKeysToScroll为false,则箭头键和空格键不会导致滚动.

If useArrowKeysToScroll is false it the arrow keys and space bars won't result in scrolling.

这篇关于一旦在用户浏览器中禁用箭头键滚动,如何启用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:55