这是我的数据表:
如果我按向下箭头,滚动条不会跟随表格的焦点,因此我必须手动移动滚动条才能看到它。我怎样才能自动执行此操作?
$(document).keydown(function(e) {
if (e.keyCode == 40) { //arrow down
if (tr.next().length) {
table.$('tr.selected').removeClass('selected');
tr.next().addClass('selected');
tr = table.$('tr.selected');
}
}
if (e.keyCode == 38) { // arrow up
if (tr.prev().length) {
table.$('tr.selected').removeClass('selected');
tr.prev().addClass('selected');
tr = table.$('tr.selected');
}
}
})
您可以在代码中添加滚动逻辑来实现这一点。
function scrollToRow(row) {
// Check if the row is not visible
if (row.position().top < 0 || row.position().top > row.parent().height()) {
// Scroll smoothly to the new row
$(Your_dropdown_table).animate({
scrollTop: row.position().top
}, 100);
}
}