$('.lorem').click(function() {
  $('.active').removeClass('active');
  $(this).addClass('active');
});
$(document).keydown(function(e){
    e.preventDefault();
  var i = $('.active').index('.lorem') + 1;
  var next = $('.lorem').eq(i);
  $('.active').removeClass('active');
  next.addClass('active');
});.lorem{
line-height:21px;
cursor:pointer;
}
.active{
background:gold;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='lorem'>lorem</div>
<div class='lorem'>lorem</div>
<div class='lorem'>lorem</div>
<div class='lorem'>lorem</div>
<div class='lorem'>lorem</div>
<div class='lorem'>lorem</div>
<div class='lorem'>lorem</div>
<div class='lorem'>lorem</div>
<div class='lorem'>lorem</div>
<div class='lorem'>lorem</div>
<div class='lorem'>lorem</div>I have the above code to select next class lorem by pressing down arrow key. 
And the similar code to select previous lorem by pressing up arrow key. 
Imagine there are a lot more lorem divs on the page (about 200).
Is there a way to scroll page automatically when the selected div is outside of viewport, so it would be inside the viewport?
