javascript - 滚动无序列表到最新的元素

标签 javascript jquery html css asp.net

每次我添加一个元素时,当它变大时,就会出现一个滚动条。但滚动条不会自动滚动到最新的元素,这意味着我希望滚动条在添加元素时自动滚动到底部。

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

--------代码省略--------------

//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
});

最佳答案

您可以设置 scrollTop 属性来强制滚动位置。试试这个:

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

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

请注意,您还可以设置此效果的动画,以便用户更明显地看出内容的位置已更改:

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

关于javascript - 滚动无序列表到最新的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30393498/

相关文章:

jquery - Div 的行为类似于使用 Angular 选择下拉列表

javascript - JS : Load User Images From Personal Drive

javascript - 更改 html 中当前事件段落的 css

javascript - 在 span JavaScript 中添加数字

javascript - 在html表格中显示json数组

javascript - 如何更改 Bot/Skype channel 传入消息文本格式

javascript - npm run watch 的问题

javascript - handle-callback-err 预期要处理的错误

javascript - 具有揭示模块模式的 ngResource promise

javascript - 如何防止我的 jQuery slider 使用 .stop() 获取 “overClicks” {或您建议使用的任何其他内容)