var liSelected; $(window).keydown(function(e) { var li = $('#hostlist .list .thumbnail'); if (e.which === 40) { // cursor down if (liSelected) { liSelected.removeClass('selected'); next = liSelected.next(); if (next.length > 0) { liSelected = next.addClass('selected'); } else { liSelected = li.eq(0).addClass('selected'); } } else { liSelected = li.eq(0).addClass('selected'); } if (liSelected) { $('body').animate({ scrollTop: liSelected.offset().top - 20 }, { duration: 500, queue: false }); } return false; } if (e.which === 38) { // cursor up if (liSelected) { liSelected.removeClass('selected'); next = liSelected.prev(); if (next.length > 0) { liSelected = next.addClass('selected'); } else { liSelected = li.last().addClass('selected'); } } else { liSelected = li.last().addClass('selected'); } if (liSelected) { $('body').animate({ scrollTop: liSelected.offset().top - 20 }, { duration: 500, queue: false }); } return false; } if (e.which === 13) { // enter if (liSelected) { window.location = liSelected.find('a').attr('href'); return false; } } if (liSelected) { liSelected.removeClass('selected'); liSelected = undefined; } if (e.which === 27) { // esc $('body').animate({scrollTop: 0}, 500); return false; } $('#search').focus(); });