scrolling to li element - jquery

前端 未结 4 977
逝去的感伤
逝去的感伤 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:21

    I got close using @lonesomeday's answer, but I found that I had to calculate the relative position of the specific li from the first li because it changed depending on the current scroll position of the containing ul.

    $('#yourUL').scrollTop($('#yourUL li:nth-child(14)').position().top - $('#yourUL li:first').position().top)
    

提交回复
热议问题