Scrolling inner div on key down and up

前端 未结 5 1867
你的背包
你的背包 2021-02-05 20:37

I have build an autocomplete container which displays the first four results and the rest are hidden and can be seen when scrolling the inner div element which holds all the res

5条回答
  •  我在风中等你
    2021-02-05 20:55

    Recently I faced the same problem and solved it by using .emphasized textscrollIntoView(), sample code is below. Example of two function respectively for up arrow key and down arrow key

    moveUp: function(e) {
            if (current > 0) {
                current--;
                var allNOdes = document.getElementById('list').childNodes;
                e.stopPropagation();
                if (allNOdes[current]) {
                    allNOdes[current].scrollIntoView(true);
                }
            }
        },
        moveDown: function(e) {
            if (current <  10) {
                current++;
                var allNOdes = document.getElementById('list').childNodes;
                e.stopPropagation();
                if (allNOdes[current]) {
                    allNOdes[current].scrollIntoView(true);
                }
            }
        }
    

    Initially define current as 0 HTML:

    1
    2
    3
    4

    CSS:

    .wrapper{
        position: relative;
        height: 150px;
        width: 100px;
        overflow: hidden;
    }
    
    .result{
        position: absolute;
    }
    

提交回复
热议问题