scrolling to li element - jquery

前端 未结 4 976
逝去的感伤
逝去的感伤 2020-12-14 03:47

i have a \"ul\" that contains lots of \"li\" (several hundred), the ul has a fixed hight of about 400px and the css property overflow:scroll, each li has the height of 40px

4条回答
  •  醉话见心
    2020-12-14 04:22

    If anyone needs a vanilla JS version, the following is working well for me, the value of 50 is just so that I'm showing the whole item when I'm near the top of the list. You can adjust that.

    function updateListSelection(liID) {
    
        var list = document.getElementById("id Tag Of The 
      element"), targetLi = document.getElementById(liID); // id tag of the
    • element list.scrollTop = (targetLi.offsetTop - 50); };

提交回复
热议问题