简体   繁体   English

滚动无序列表到最新项目

[英]Scroll Unordered List to the latest item

Each time i add a item, the moment it becomes big, a scroll bar appears. 每当我添加一个项目时,当它变大时,就会出现一个滚动条。 But the scroll bar is not auto scrolling to the latest item which means i want the scroll bar to auto scroll to the bottom whenever i add an item. 但是滚动条不会自动滚动到最新的项目,这意味着我希望滚动条在我添加项目时自动滚动到底部。

<ul id ="list" style ="overflow:auto; height:300px;"></ul>

--------code omitted-------------- --------省略代码--------

//Register sendButton Click Event
$("#sendButton").click(function () {
    hubProxy.server.send($("#inputTextBox").val());
    $("#inputTextBox").val("").focus();
    //HERE  i want it to scroll to the bottom most
});

You can set the scrollTop property to force the scroll position. 您可以设置scrollTop属性以强制滚动位置。 Try this: 尝试这个:

$("#sendButton").click(function () {
    hubProxy.server.send($("#inputTextBox").val());
    $("#inputTextBox").val("").focus();

    $('#list').scrollTop($('#list').height());
});

Note that you can also animate this effect so that it's more obvious to the user that the location of the content has changed: 请注意,您还可以设置动画效果,以便用户更清楚地看到内容的位置已更改:

$('#list').animate({ scrollTop: $('#list').height() }, "slow");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM