简体   繁体   English

如何使用箭头键导航对溢出列表进行自动滚动?

[英]How to make autoscroll on overflowed list with arrow keys navigation?

I made arrow navigation for my list, but the problem is my list has scroll and selected item finally goes down beyond the limits of my container. 我为列表做了箭头导航,但是问题是我的列表滚动了,并且所选项目最终超出了容器的限制。 It would be perfect if my container's scroll was going down with the selected item. 如果我的容器的滚动与所选项目一起下降,那将是完美的。 Can somebody hint me how to solve this? 有人可以提示我如何解决这个问题吗?

  $(function() { $(document).on('keydown', function(e) { switch (e.which) { case 40: e.preventDefault(); $('li:not(:last-child).selected').removeClass('selected') .next() .addClass('selected'); break; case 38: e.preventDefault(); $('li:not(:first-child).selected').removeClass('selected') .prev() .addClass('selected'); } }) }) 
 ul { padding: 0; list-style: none; max-height: 100px; overflow: scroll; } li.selected { background-color: orange; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li class="selected">item #1</li> <li>item #2</li> <li>item #3</li> <li>item #4</li> <li>item #5</li> <li>item #6</li> <li>item #7</li> <li>item #8</li> <li>item #9</li> <li>item #10</li> <li>item #11</li> </ul> 

  $(function() { $(document).on('keydown', function(e) { switch (e.which) { case 40: e.preventDefault(); $('li:not(:last-child).selected').removeClass('selected') .next() .addClass('selected'); break; case 38: e.preventDefault(); $('li:not(:first-child).selected').removeClass('selected') .prev() .addClass('selected'); } //scroll to element: $("ul").scrollTop(0);//set to top $("ul").scrollTop($('li.selected').offset().top - $('li.selected').height()); }) }) 
 ul { padding: 0; list-style: none; max-height: 100px; overflow: scroll; } li.selected { background-color: orange; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li class="selected">item #1</li> <li>item #2</li> <li>item #3</li> <li>item #4</li> <li>item #5</li> <li>item #6</li> <li>item #7</li> <li>item #8</li> <li>item #9</li> <li>item #10</li> <li>item #11</li> </ul> 

You can try above snippet. 您可以尝试上面的代码段。 To set the scroll to the selected li element. 将滚动条设置到选定的li元素。

You can also add tabindex to all list items. 您还可以将tabindex添加到所有列表项。 Give focus to selected element. 将焦点放在所选元素上。 That should solve your problem. 那应该解决您的问题。

 $(function() { $('li').attr('tabindex', 0); $(document).on('keydown', function(e) { switch (e.which) { case 40: e.preventDefault(); $('li:not(:last-child).selected').removeClass('selected') .next() .addClass('selected') .focus(); break; case 38: e.preventDefault(); $('li:not(:first-child).selected').removeClass('selected') .prev() .addClass('selected') .focus(); } }) }) 
 ul { padding: 0; list-style: none; max-height: 100px; overflow: scroll; } li.selected { background-color: orange; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li class="selected">item #1</li> <li>item #2</li> <li>item #3</li> <li>item #4</li> <li>item #5</li> <li>item #6</li> <li>item #7</li> <li>item #8</li> <li>item #9</li> <li>item #10</li> <li>item #11</li> </ul> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM