Prevent window scroll jquery

2019-07-01 13:28发布

I'm developing a select menu replacement in jquery.
First I've to make the new select menu focusable by just adding tabindex="0" to the container.
Then, I disable focus on the original select menu and give focus to the new one. When the new one is focused and you press the up and down arrows the options change accordingly but there's a big problem. As you press the arrows the body moves too.
I tried all these solutions so far with no luck:

$(window).unbind('scroll');
$(document).unbind('scroll');
$('body').unbind('scroll');
$(window).unbind('keydown');
$(document).unbind('keydown');

Check the code here http://pastebin.com/pVNMqyui This code is from the development version of Ideal Forms http://code.google.com/p/idealforms that I'm about to release soon, with keyboard support.

Any ideas why this is not working?

EDIT: Solved!

Found the answer on this post jquery link tag enable disable

var disableScroll = function(e){
    if (e.keyCode === 40 || e.keyCode === 38) {
        e.preventDefault();
        return false;
    }
};
// And then...
events.focus: function(){ $(window).on('keydown', disableScroll); }
events.blur: function(){ $(window).off('keydown', disableScroll); }

It works!

5条回答
不美不萌又怎样
2楼-- · 2019-07-01 14:13

Its very simple.you need not even need jQuery for this.

jQuery:

$("body").css("overflow", "hidden");

javascript

<body style="overflow: hidden">

Adding in style:

<style>
 body {width:100%; height:100%; overflow:hidden, margin:0}
 html {width:100%; height:100%; overflow:hidden}
</style>

if you want to bind the arrow keys,try something like:

$('body').keydown(function(e){
    e.preventDefult();
    if(e.keyCode == 37) // for left key
    {
        // implement focus functionality
    }
    if(e.keyCode == 38) // for up key
    {
        // implement focus functionality
    }
    if(e.keyCode == 39) // for right key
    {
        // implement focus functionality
    }
    if(e.keyCode == 40) // for doqn key
    {
        // implement focus functionality
    }
});
查看更多
一夜七次
3楼-- · 2019-07-01 14:20

Found the answer on this post jquery link tag enable disable

var disableScroll = function(e){
    if (e.keyCode === 40 || e.keyCode === 38) {
        e.preventDefault();
        return false;
    }
};
// And then...
events.focus: function(){ $(window).on('keydown', disableScroll); }
events.blur: function(){ $(window).off('keydown', disableScroll); }
查看更多
聊天终结者
4楼-- · 2019-07-01 14:24

In your keydown handler, for up and down keys, return false like this:

'keydown' : function (e) {
    if (e.keyCode === 40) { // Down arrow
        that.events.moveOne('down');
    }
    if (e.keyCode === 38) { // Up arrow
        that.events.moveOne('up');
    }
    return false;
}

Also, make sure this return gets propagated to the browser's native onkeydown depending on how/which framework you're using.

查看更多
forever°为你锁心
5楼-- · 2019-07-01 14:25

You need to cancel the keydown event for arrow keys. Use either e.preventDefault() or return false in your .keydown() handler if an arrow key has been pressed.

查看更多
一纸荒年 Trace。
6楼-- · 2019-07-01 14:33

The Best way to achive the same is to set overflow of the body to hidden

$("body").css("overflow", "hidden");

After the process just do the opposite

`$("body").css("overflow", "hidden");
查看更多
登录 后发表回答