Skip to content

Commit 313b5fe

Browse files
committed
Fix scrollbar position in menu when moving up and down with keyboard
fixes #154
1 parent 1436e73 commit 313b5fe

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

js/bootstrap-combobox.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,17 +327,34 @@
327327
case 38: // up arrow
328328
e.preventDefault();
329329
this.prev();
330+
this.fixMenuScroll();
330331
break;
331332

332333
case 40: // down arrow
333334
e.preventDefault();
334335
this.next();
336+
this.fixMenuScroll();
335337
break;
336338
}
337339

338340
e.stopPropagation();
339341
}
340-
342+
343+
, fixMenuScroll: function(){
344+
var active = this.$menu.find('.active');
345+
if(active.length){
346+
var top = active.position().top;
347+
var bottom = top + active.height();
348+
var scrollTop = this.$menu.scrollTop();
349+
var menuHeight = this.$menu.height();
350+
if(bottom > menuHeight){
351+
this.$menu.scrollTop(scrollTop + bottom - menuHeight);
352+
} else if(top < 0){
353+
this.$menu.scrollTop(scrollTop + top);
354+
}
355+
}
356+
}
357+
341358
, keydown: function (e) {
342359
this.suppressKeyPressRepeat = ~$.inArray(e.keyCode, [40,38,9,13,27]);
343360
this.move(e);

0 commit comments

Comments
 (0)