DIV中的长UL列表,自动滚动到LI元素

kub*_*uba 3 html javascript css jquery

我有这么奇怪的问题,我有一个div

_inputHelper // it has fixed height ( 200px )
Run Code Online (Sandbox Code Playgroud)

有非常非常长的<ul>列表(大约200个元素),我想使用jQuery滚动到<li>列表中的一些,但问题是,当我以这样的方式滚动整个列表时:

$( _inputHelper.find('ul') ).animate( { top : -1 * ( $(_this).position().top - $(_this).height() ) } , 200);
Run Code Online (Sandbox Code Playgroud)
_this is a LI element I would like to scroll to
Run Code Online (Sandbox Code Playgroud)

在适当的位置向下滚动,但我无法使用鼠标滚动列表,这里是一个更精确的打印屏幕:

在div自动滚动问题中的长UL

我想我可能会以错误的方式滚动,但我也尝试过scrollTop/margin-top,它仍然无效.

一些测试:http://jsfiddle.net/uk5xqfry/3/

有帮助吗?

Dan*_*niP 5

基于你的小提琴你需要使用scrollTop,试试这个:

$('.input-helper').animate({
    scrollTop : $("#test").position().top,
}, 200);
Run Code Online (Sandbox Code Playgroud)

检查演示小提琴


在您的代码中,您只是移动ul元素,然后超出父视图; 现在,您正在更改父级的滚动.