Elaborating on my comment
Set the container's scrollTop
to index of focused li
* li height
.
return false
upon keydown to prevent normal browser scrolling of overflown parent.
$('div.container').on('focus', 'li', function() {
var $this = $(this);
$this.addClass('active').siblings().removeClass();
$this.closest('div.container').scrollTop($this.index() * $this.outerHeight());
}).on('keydown', 'li', function(e) {
var $this = $(this);
if (e.keyCode === 40) {
$this.next().focus();
return false;
} else if (e.keyCode === 38) {
$this.prev().focus();
return false;
}
}).find('li').first().focus();
jsfiddle http://jsfiddle.net/38zR3/42/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…