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

javascript - How to disable page scrolling in FF with arrow keys - Stack Overflow

programmeradmin3浏览0评论

I'm building a menu with topics and items. Each topic can be expanded and collapsed by clicking on it. My task is to make it possible to move through menu topics and items with the up and down arrow keys. I've done this already, but the problem is that when the page is bigger than the window, the page is scrolling when pressing the arrow keys. I've tried using:

document.body.style.overflow = "hidden";

to stop the page from scrolling. Thus when I click on 'Topic2' for example I can continue using the arrow keys to go to next topic/item. After that if I click anywhere else on the screen I set the overflow back to auto and the page can be scrolled again.

This works in IE, but not in FF. In FF the scrollbars are being removed and the mousewheel doesn't scroll the page, but the arrow keys still DO. So my question is how to fix that, or better, how not to scroll the page when the focus is on any menu element? Thus I won't use the overflow property.

I'm building a menu with topics and items. Each topic can be expanded and collapsed by clicking on it. My task is to make it possible to move through menu topics and items with the up and down arrow keys. I've done this already, but the problem is that when the page is bigger than the window, the page is scrolling when pressing the arrow keys. I've tried using:

document.body.style.overflow = "hidden";

to stop the page from scrolling. Thus when I click on 'Topic2' for example I can continue using the arrow keys to go to next topic/item. After that if I click anywhere else on the screen I set the overflow back to auto and the page can be scrolled again.

This works in IE, but not in FF. In FF the scrollbars are being removed and the mousewheel doesn't scroll the page, but the arrow keys still DO. So my question is how to fix that, or better, how not to scroll the page when the focus is on any menu element? Thus I won't use the overflow property.

Share Improve this question edited May 2, 2012 at 6:11 user 87.1k18 gold badges199 silver badges190 bronze badges asked Jan 7, 2010 at 13:02 ChavdarChavdar 331 silver badge3 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 12

You have to bind a keydown event to the document, and if the event keycode matches any of the arrow keys (37 through 40), return false. That way the arrow press won't go any further.

document.onkeydown = function(e) {
    var k = e.keyCode;
    if(k >= 37 && k <= 40) {
        return false;
    }
}

You can easily expand on that to work only when your menu is active, but without seeing some of it's code, it's impossible to give you an example.

below code has fixed the problem

$(window).scroll(function () { 
  window.scrollTo(0,0);
});

The only way I can see is intercepting the keydown event and doing the blurring/focusing yourself.

There seem to be some gotchas with catching those keys, see this question for a number of (JQuery based) examples that look quite promising.

发布评论

评论列表(0)

  1. 暂无评论