Skip to content
Advertisement

Horizontal scroll using arrow keys

I use a horizontal scroll on a page I built. It’s inside a div, named it with a class, and I want to use the arrows keys to scroll. For it to scroll with the keys, I need to click somewhere on it.

Is it possible to use the keys directly on first load of the page without necessarily clicking it?

I need the arrow keys to be functional directly and start the horizontal scroll inside that particular div, if that’s possible.

JavaScript

Advertisement

Answer

OK – here is an example of what I mean. If we set the div to have tabindex=0 and add a script to the end of the tbody element that sets the focus to the div, the left/right cursors keys work on the div scrollbar.

JavaScript
JavaScript

To use a class, here is my entire test page:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement