如何使列表框中的选定项目滚动到顶部? [英] How can I make selected item in listbox scroll to the top?

查看:69
本文介绍了如何使列表框中的选定项目滚动到顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个由数组项组成的简单列表.如果列表很大,将显示滚动条.但是,我的问题是-选择一个项目时,我希望看到该项目到可见位置的顶部,将所有隐藏在滚动条下方的上方所有项目推入.因此,无论我选择什么项目,它都应该位于可见列表框的顶部.这与将项目放置在顶部(仅在滚动顶部)不同.希望我的问题在这里得到理解.

感谢我能提供的任何帮助或指导.

解决方案

您可以使用以下代码:

$('#scrollable li').on('click', function(e){
    $('#scrollable').animate({scrollTop: $(e.target).position().top + $('#scrollable').scrollTop()});
});

另请参见以下JSFiddle: http://jsfiddle.net/729nX/1/

I creating a simple list off of array items. Scroll bars will appear if the list is big. However, my question is - when an item is selected, I would like to see that item to the top of the visible position, pushing all above items hidden above under scrollbar. So no matter what I item I select, it should be on top of the visible list box. This is not same as positioning the item to the top, just on the scrolling top. Hope my question is understood here.

Thanks for any help or direction I can get.

解决方案

You can use this code:

$('#scrollable li').on('click', function(e){
    $('#scrollable').animate({scrollTop: $(e.target).position().top + $('#scrollable').scrollTop()});
});

See also this JSFiddle: http://jsfiddle.net/729nX/1/

这篇关于如何使列表框中的选定项目滚动到顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆