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

I have a div ponent with an inner scrollbar and I'd like to prevent the updown arrow keys from sc

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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745377893a4625085.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信