Is it possible to prevent users Page Up, Page Down

2019-07-07 00:32发布

问题:

Using in html body

overflow: hidden

I managed to control the scroll flow of a webpage. Is it possible to also prevent users Page Up, Page Down, Up and Down arrow keys from scroll functioning?

回答1:

Use preventDefault():

window.onkeydown=function(e){
   if(e.keycode==33 || e.keycode==34 || e.keycode==38 || e.keycode==40){
       e.preventDefault();
   }
}


回答2:

This was my solution:

var ar = new Array(33, 34, 38, 40);

$(document).keydown(function (e) {
    var key = e.which;
    if ($.inArray(key, ar) > -1) {
        e.preventDefault();
        return false;
    }
    return true;
});


回答3:

37 - left 38 - up 39 - right 40 - down

$(document).keydown(function(e){
   if(e.which>=36 && e.which<40){
       e.preventDefault();
   }
});

DEMO: FIDDLE