JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.js"></script>
Type <b>a</b> to start autocomplete<br>
use keyboard to select from autocompletion list<br>
<span id="notice">notice visual "glitch" when keyboard scrolling up and down</span><br>
<input id="autocomplete" type="text">
<span id="fix">Click here to fix autocomplete</span>
CSS
.ui-autocomplete {
position: absolute;
max-height: 100px;
overflow-y: auto; overflow-x: hidden;
border: 4px double #777;
background-color: #ccc;
}
li a {
display: block; line-height: 16px;
height: 18px; border: 1px solid #ccc;
}
li a.ui-state-hover {
background-color: #ddd;
border: 1px solid #f77;
}
span#fix {cursor: pointer;}
span#fix:hover {background-color: #ccc;}
JavaScript
$('#autocomplete').autocomplete({
source: ['a1','a2','a3','a4','a5','a6','a7','a8',
'a9','a10','a11','a12'],
minLength: 0
}).focus();
$('#fix').click(function() {
$('#autocomplete').autocomplete('widget').data('menu').activate =
// this function is copied from jquery.ui.autocomplete 1.8.12
// ui.menu activate
function(event, item) {
this.deactivate();
if (this.hasScroll()) {
// patched line to add menu border width checking
var offset = item.offset().top - (this.element.offset().top + (parseInt(this.element.css('border-top-width')) || 0)),
scroll = this.element.attr("scrollTop"),
elementHeight = this.element.height();
if (offset < 0) {
this.element.attr("scrollTop", scroll + offset);
} else if (offset >= elementHeight) {
this.element.attr("scrollTop", scroll + offset - elementHeight + item.height());
}
}
this.active = item.eq(0).children("a").addClass("ui-state-hover").
attr("id", "ui-active-menuitem").end();
this._trigger("focus", event, { item: item });
};
alert('Fixed');
$('#autocomplete').val('').focus();
$('#notice').text('keyboard scrolling results should now be perfectly aligned');
$(this).remove();
});