Skip to content

Commit 5933ce4

Browse files
committed
Merge pull request #182 from lphooge/fix-keyboard-scrolling
Added keyboard scrolling and handler function
2 parents 8e0b0af + 408b39f commit 5933ce4

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

js/bootstrap-combobox.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,17 +330,34 @@
330330
case 38: // up arrow
331331
e.preventDefault();
332332
this.prev();
333+
this.fixMenuScroll();
333334
break;
334335

335336
case 40: // down arrow
336337
e.preventDefault();
337338
this.next();
339+
this.fixMenuScroll();
338340
break;
339341
}
340342

341343
e.stopPropagation();
342344
}
343345

346+
, fixMenuScroll: function(){
347+
var active = this.$menu.find('.active');
348+
if(active.length){
349+
var top = active.position().top;
350+
var bottom = top + active.height();
351+
var scrollTop = this.$menu.scrollTop();
352+
var menuHeight = this.$menu.height();
353+
if(bottom > menuHeight){
354+
this.$menu.scrollTop(scrollTop + bottom - menuHeight);
355+
} else if(top < 0){
356+
this.$menu.scrollTop(scrollTop + top);
357+
}
358+
}
359+
}
360+
344361
, keydown: function (e) {
345362
this.suppressKeyPressRepeat = ~$.inArray(e.keyCode, [40,38,9,13,27]);
346363
this.move(e);

0 commit comments

Comments
 (0)