javascript 如何滚动到 ul 的底部?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15757144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:00:08  来源:igfitidea点击:

How to scroll to bottom of the ul?

javascriptjqueryhtmljquery-uicordova

提问by SuReSh PaTi

I have developed one application using PhoneGap. In my application I have displayed a number of elements in a listview using uili. Here I want to scroll to the last element in list. For that i have used following code,

我使用 PhoneGap 开发了一个应用程序。在我的应用程序中,我使用uili. 在这里,我想滚动到列表中的最后一个元素。为此,我使用了以下代码,

$('#dates li').last().addClass('active-li').focus();
$('#dates li').last().focus();

I have used this one also,

我也用过这个,

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

extra class is adding but focus is not working.

额外的课程正在添加,但焦点不起作用。

回答by u283863

Add the tabindexattribute:

添加tabindex属性:

<li tabindex="1"></li>

http://jsfiddle.net/DerekL/GEsmb/

http://jsfiddle.net/DerekL/GEsmb/

Because you can not focus on li. It's not a text field or button or something like that.

因为你无法专注li。它不是文本字段或按钮或类似的东西。

回答by Praveen Kumar Purushothaman

You can use this code:

您可以使用此代码:

$(document).ready(function(){
    $('body').scrollTop($('ul li').last().position().top + $('ul li').last().height());
});

Fiddle: http://jsfiddle.net/praveenscience/GEsmb/1/

小提琴:http: //jsfiddle.net/praveenscience/GEsmb/1/

回答by Eli

Try scrollTop:

尝试滚动顶部

$('#dates').scrollTop($('#dates').height())

or:

或者:

$('#dates').scrollTop($('#dates')[0].scrollHeight);

if your list has lots of content.

如果您的列表有很多内容。