I have a ul
that I am scrolling up and down on. The ul
has overflow:auto
on it, so after a certain point the selected item is no longer visible. You can see an example of the problem at this link: http://jsfiddle.net/NjC58/34/
Currently I have the following code, borrowed largely from this post:
var displayBoxIndex = -1;
var Navigate = function (diff) {
displayBoxIndex += diff;
var oBoxCollection = $("#leftDrop li");
if (displayBoxIndex >= oBoxCollection.length) {
displayBoxIndex = 0;
}
if (displayBoxIndex < 0) {
displayBoxIndex = oBoxCollection.length - 1;
}
var cssClass = "selectedInitialNav";
oBoxCollection.removeClass(cssClass).eq(displayBoxIndex).addClass(cssClass);
}
$(document).keydown(function(e){ // 38-up, 40-down
if (e.keyCode == 40) {
Navigate(1);
return false;
}
if (e.keyCode == 38) {
Navigate(-1);
return false;
}
});
Thus, I am looking for a solution that will scroll the overflow
to the top if you are pressing down arrow
and get to the bottom of the visible ul
, and if you are pressing up arrow
and get to the top of the visible ul
will scroll overflow
down so that the selected li
is at the bottom of the visible ul
.
I have been looking for solutions using jQuery's scroll function, using this post, and this post (which mentions scrollTop
), but haven't actually found anything close to a solution yet.