I have build an autocomplete container which displays the first four results and the rest are hidden and can be seen when scrolling the inner div element which holds all the results.
I have implemented the on key up when pressing the up and down keys in order to let the users navigate easily through the results but the inner div with the results isn't scrolling.
How can you scroll an element which has overflow-y:hidden
and not the window ?
In the example , just press any key inside the input box and use your arrows to go down, you will see that the div isn't scrolling
Recently I faced the same problem and solved it by using .emphasized textscrollIntoView(), sample code is below. Example of two function respectively for up arrow key and down arrow key
Initially define current as 0 HTML:
CSS:
By using
tabIndex="-1"
attribute on each of the children of a container, the browser will automatically scroll the container to have the child with the current focus in-view.Demo (vanilla javascript):
To make this list accessible (ARIA) read this
Two years passed... just in case someone stumbles over this, like me. In addition to the last two lines in Moobs answer (can't do any comments yet...), plain Javascript:
(Left out: how to get elements in pure JS), then:
$innerDiv.scrollTop = $hoveredElement.offsetTop - $innerDiv.clientHeight;
There also seems to be no need to "reset"
scrollTop
to zero before assigning the new valueYou can update your script to find the relative position of the selected element and scroll to it:
http://jsfiddle.net/kMzR9/3/
There are several ways to implement it. and the exact solution for you need to consider your context.
Anyway, one possible solution is to use in the container div 'position: relative' and in the inner div (which hold the content) use 'position: absolute' and 'top: 0px'. When user press on the up/down arrows you changing the top property accordingly.
CSS:
JavaScript:
HTML:
See my example in: http://jsfiddle.net/Kq2Qq/3/