简体   繁体   中英

keyboard scrolling UP / DOWN

I have this code

$(document.body).onkeyup = function(e) {
    var code = e.keyCode;
    if(code === 74) {
        window.scrollTo(document.body.scrollLeft,
                    document.body.scrollTop + 200);
    }
};

that allows me to scroll a page up and down with the keyboard. The problem is that I have to click on the page I want to scroll to activate this function.

Is there a way to activate immediately without the mouse click?

Thanks.

As @somethinghere mentioned in the comments, the reason the shortcut doesn't work is because the document doesn't have focus.

You can attempt to force focus to document , but doing so is browser dependant:
Is there any way in JavaScript to focus the document (content area)?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM