Skip to content Skip to sidebar Skip to footer

Is It Possible To Prevent Users Page Up, Page Down, Up And Down Arrow Keys From Scroll Functioning?

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

Solution 1:

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;
});

Solution 2:

Use preventDefault():

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

Solution 3:

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

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

DEMO: FIDDLE


Post a Comment for "Is It Possible To Prevent Users Page Up, Page Down, Up And Down Arrow Keys From Scroll Functioning?"