i'm trying to apply automatic scroll in div. I have a list of element under fixed height div now i want when i press key down after 3rd highlight li element (i.e Compt0005) div scroll automatically. can any body solve it..
Demo
:- here
i'm trying to apply automatic scroll in div. I have a list of element under fixed height div now i want when i press key down after 3rd highlight li element (i.e Compt0005) div scroll automatically. can any body solve it..
Demo
:- here
change every occurence of
$("#ulSearch li:eq(" + liPossition + ")").css("background-color", "red")
to
$("#ulSearch li:eq(" + liPossition + ")").css("background-color", "red")[0].scrollIntoView();
This code sets the scrollTop property of the div exactly via .getBoundingClientRect() method:
$(function () {
var liPossition = 0;
var items = $("#ulSearch li");
var divSearchContent = $("#divSearchContent");
divSearchContent.hide();
$("#txtSearch").keydown(function (e) {
if (e.keyCode == 13) { // if enter pressed
$("#txtSearch").val(items.eq(liPossition).text());
divSearchContent.hide();
return;
}
if (e.keyCode != 38 && e.keyCode != 40) { // // if not up or down arrow
return;
}
divSearchContent.show();
items.eq(liPossition).css("background-color", "");
if (e.keyCode == 40 && liPossition < items.length - 1) { // down arrow key code
liPossition++;
}
if (e.keyCode == 38 && liPossition > 0) { // up arrow key code
liPossition--;
}
items.eq(liPossition).css("background-color", "red");//[0].scrollIntoView(); return;
var b1 = divSearchContent[0].getBoundingClientRect();
var b2 = items.eq(liPossition)[0].getBoundingClientRect();
if (b1.top>b2.top) {
divSearchContent.scrollTop( divSearchContent.scrollTop()-b1.top+b2.top );
}
else if (b1.bottom<b2.bottom) {
divSearchContent.scrollTop( divSearchContent.scrollTop()-b1.bottom+b2.bottom );
}
});
});