scrolling to li element - jquery

前端 未结 4 971
逝去的感伤
逝去的感伤 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 03:57

    You need to do something like this:

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

    If you want to animate it, do

    $('#yourUL').animate({
         scrollTop: $('#yourUL li:nth-child(14)').position().top
    }, 'slow');
    

    Obviously adjust 14 for different lis.

提交回复
热议问题