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

javascript - Prevent focused div from scrolling with arrow keys - Stack Overflow

programmeradmin1浏览0评论

I have a div ponent with an inner scrollbar and I'd like to prevent the up/down arrow keys from scrolling when the element is focused (mouse click on the element), as they are used for other events (e.g. zoom).

The only solution I found was to attach an event listener to the document, however, it disables all default arrow keys events, such as moving the cursor in an input field.

Here is an example (in React):

How to reproduce:

  • Click with the mouse on the inner div (with the text)
  • Click the down arrow key -> div scrolls down

Is there a way to prevent the scroll without disabling it on the document level?

Thanks!

UPDATE:

This missing piece was adding tab-index='0' to the ponent (I updated the code above). Thanks @Jarvan

I have a div ponent with an inner scrollbar and I'd like to prevent the up/down arrow keys from scrolling when the element is focused (mouse click on the element), as they are used for other events (e.g. zoom).

The only solution I found was to attach an event listener to the document, however, it disables all default arrow keys events, such as moving the cursor in an input field.

Here is an example (in React): https://codesandbox.io/s/rsc-live-example-fze6z

How to reproduce:

  • Click with the mouse on the inner div (with the text)
  • Click the down arrow key -> div scrolls down

Is there a way to prevent the scroll without disabling it on the document level?

Thanks!

UPDATE:

This missing piece was adding tab-index='0' to the ponent (I updated the code above). Thanks @Jarvan

Share Improve this question edited Aug 8, 2022 at 8:23 vsync 131k59 gold badges340 silver badges423 bronze badges asked Oct 24, 2019 at 4:54 RaniRani 6,8222 gold badges27 silver badges33 bronze badges 2
  • Seems your example is working, what is that you were not able to solve ? – Narkhede Tushar Commented Oct 24, 2019 at 5:13
  • 1 @NarkhedeTushar when you click on the div and then the down arrow key it scrolls the div. I want to prevent that – Rani Commented Oct 24, 2019 at 5:43
Add a ment  | 

1 Answer 1

Reset to default 4

You can filter the events which you don't want to prevent, such as arrow event in input.

if (e.target.tagName.toLowerCase()!=="input" &&(e.key === "ArrowUp" || e.key === "ArrowDown")) {
          e.preventDefault();
}

Then in input the cursor move will not be prevented.

But it won't work if the event you want to keep is some behavior in same element.

E.g. you have some children can be focus in the 'prevent arrow up/down scroll' div, you want to keep the ability to move focus between children by using arrow up/down. In that case I guess you have to implement you customize scrollbar then you have the full control, because in browser behavior I don't see a way to separate the single 'scroll' action.

Update:

If you know which ponent or area you want to effect, just add to that area. Like in your demo code:

<div tabindex="0"
      onKeyDown={e => {
        console.log(e);
        if (e.key === "ArrowUp" || e.key === "ArrowDown") {
          e.stopPropagation();
          e.preventDefault();
          console.log(e.key);
          return false;
        }
        e.stopPropagation();
      }}
    >
      {result}
    </div>
发布评论

评论列表(0)

  1. 暂无评论