最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - ul list not scrolling on key press but it does with mouse wheel - Stack Overflow

matteradmin6PV0评论

I'm working with Bootstrap 3 and i have an autossugest input. The problem is that i want the <ul> to scroll with the keyboard keys but it doesn't work. I think scrolling with arrows keys is a default behavior but the <ul> doesn´t do it. Here is what is happening:

If i press the down key twice:

I'm using the typeahead developed by Bassjobsen.

HTML code:

<input type="text" class="form-control" data-provide="typeahead" id="test">

Javascript (with JQuery.js) code:

$('document').ready (function () {

  $('#test').typeahead({
      source: ['algo', 'pepe', 'algo2', 'algo34', 'pepe3', 'algo42'],
      items: 'all',
      minLength: 2
  });

});

I set items to all to show all items in source. That's why i need to scroll them.

I added this inline style to the generated <ul>:

style="max-height: 50px; overflow-y: auto;"

So this is the code generated by Bassjobsens library:

<ul class=" dropdown-menu" style="max-height: 50px; overflow-y: auto; top: 73px; left: 20px; display: none;" "="">
  <li class="active">
     <a href="#"><strong>al</strong>go</a>
  </li>
  <li>
     <a href="#"><strong>al</strong>go2</a>
  </li>
  <li>
     <a href="#"><strong>al</strong>go34</a>
  </li>
  <li>
     <a href="#"><strong>al</strong>go42</a>
  </li>
</ul>

I'm working with Bootstrap 3 and i have an autossugest input. The problem is that i want the <ul> to scroll with the keyboard keys but it doesn't work. I think scrolling with arrows keys is a default behavior but the <ul> doesn´t do it. Here is what is happening:

If i press the down key twice:

I'm using the typeahead developed by Bassjobsen.

HTML code:

<input type="text" class="form-control" data-provide="typeahead" id="test">

Javascript (with JQuery.js) code:

$('document').ready (function () {

  $('#test').typeahead({
      source: ['algo', 'pepe', 'algo2', 'algo34', 'pepe3', 'algo42'],
      items: 'all',
      minLength: 2
  });

});

I set items to all to show all items in source. That's why i need to scroll them.

I added this inline style to the generated <ul>:

style="max-height: 50px; overflow-y: auto;"

So this is the code generated by Bassjobsens library:

<ul class=" dropdown-menu" style="max-height: 50px; overflow-y: auto; top: 73px; left: 20px; display: none;" "="">
  <li class="active">
     <a href="#"><strong>al</strong>go</a>
  </li>
  <li>
     <a href="#"><strong>al</strong>go2</a>
  </li>
  <li>
     <a href="#"><strong>al</strong>go34</a>
  </li>
  <li>
     <a href="#"><strong>al</strong>go42</a>
  </li>
</ul>
Share Improve this question edited Apr 3, 2014 at 1:55 Fernando Prieto asked Apr 1, 2014 at 12:49 Fernando PrietoFernando Prieto 5201 gold badge6 silver badges20 bronze badges 2
  • Any updates on that issue? – anvarik Commented Dec 8, 2014 at 15:42
  • Have you fix it? – askingtoomuch Commented Jun 24, 2021 at 12:59
Add a ment  | 

1 Answer 1

Reset to default 3

Final Edit:

Alright, I started having too much fun figuring this out, and finally have (hopefully you still need the solution!) Here is a working solution, building off of what I posted earlier. I hope it's what you were looking for!

$('#test').keydown(function(e) {
    if($('.dropdown-menu').is(':visible')) {

        var menu = $('.dropdown-menu');
        var active = menu.find('.active');
        var height = active.outerHeight(); //Height of <li>
        var top = menu.scrollTop(); //Current top of scroll window
        var menuHeight = menu[0].scrollHeight; //Full height of <ul>

        //Up
        if(e.keyCode == 38) {
            if(top != 0) {
                //All but top item goes up
                menu.scrollTop(top - height);
            } else {
                //Top item - go to bottom of menu
                menu.scrollTop(menuHeight + height);
            }
        }    
        //Down
        if(e.keyCode == 40) {
            //window.alert(menuHeight - height);
            var nextHeight = top + height; //Next scrollTop height
            var maxHeight = menuHeight - height; //Max scrollTop height

            if(nextHeight <= maxHeight) {
                //All but bottom item goes down
                menu.scrollTop(top + height);
            } else {
                //Bottom item - go to top of menu
                menu.scrollTop(0);
            }
        }
    }
});
Post a comment

comment list (0)

  1. No comments so far