2
这是我找到的解决方案stackoverflow 这种解决方案适用于我,除了当div的内容溢出时,内容不会滚动旁边的箭头键,如滚动似乎是5个或6行后慢得多。使用箭头键导航divs的网格wount滚动溢出的内容
请这里的上述
var position = { x: 0, y: 0 };
var calendarMap = [];
$(document).ready(function() {
$('.row').each(function() {
calendarMap.push([]);
$('.day, .date', this).each(function() {
calendarMap[calendarMap.length - 1].push($(this));
});
});
highlightCell();
});
$(window).on('keydown', function (e) {
if (e.keyCode === 37) // left
moveLeft();
else if (e.keyCode === 38) // up
moveUp();
else if (e.keyCode === 39) // right
moveRight();
else if (e.keyCode === 40) // down
moveDown();
highlightCell();
});
function moveLeft() {
position.x--;
if (position.x < 0)
position.x = 0;
}
function moveUp() {
position.y--;
if (position.y < 0)
position.y = 0;
}
function moveRight() {
position.x++;
if (position.x >= calendarMap[0].length)
position.x = calendarMap[0].length - 1;
}
function moveDown() {
position.y++;
if (position.y >= calendarMap.length)
position.y = calendarMap.length - 1;
}
function highlightCell() {
$('.day, .date').removeClass('selected');
calendarMap[position.y][position.x].addClass('selected');
}