最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Prevent window scroll jquery - Stack Overflow

programmeradmin0浏览0评论

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 This code is from the development version of Ideal Forms 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!

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./pVNMqyui This code is from the development version of Ideal Forms http://code.google./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!

Share Improve this question edited May 23, 2017 at 10:34 CommunityBot 11 silver badge asked Dec 13, 2011 at 7:22 elclanrselclanrs 94.2k21 gold badges137 silver badges171 bronze badges 2
  • perhaps this is a job for event.preventDefault(); in the handler for any event that might cause the window to scroll... – Aaron Commented Dec 13, 2011 at 7:28
  • 2 You should add this as an answer to your own question. – Merlyn Morgan-Graham Commented Dec 13, 2011 at 8:07
Add a ment  | 

5 Answers 5

Reset to default 2

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.

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

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.

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

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");
发布评论

评论列表(0)

  1. 暂无评论