jquery - 滚动到具有动态内容的 div 的底部

标签 jquery html css

我有一个带有动态内容的固定高度 div。当一些内容添加到 div 时,我希望自动滚动到底部。

我在 SO 上找到了 scrollTop = scrollHeight 解决方案,但它在我的情况下不起作用。

$(document).ready(function(){
  var text = '<p>some text</p><p>some text</p><p>some text</p><p>some text</p><p>some text</p><p>some text</p><p>some text</p><p>some text</p><p>some text</p><p>some text</p>'
  
  $("#content").html(text);
  $("#content").scrollTop($('#content').scrollHeight)
})
#content{
  max-height:250px;
  overflow-y:auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content"></div>

最佳答案

改变

$("#content").scrollTop($('#content').scrollHeight)

$("#content").scrollTop($('#content')[0].scrollHeight)

关于jquery - 滚动到具有动态内容的 div 的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45415437/

相关文章:

javascript - jQuery 巨型菜单仅显示单击的父子菜单

javascript - 添加 Angular 自定义指令到动态生成的 DOM?

jquery - 有没有一种方法可以在不使用绝对位置的情况下将 div 放置在彼此之上?

jquery - 透明背景在 IE9 中无法正常工作

javascript - 在 JavaScript 中单击一次删除多个项目

jquery - 基于另一个 div 的 css 使用 jQuery 更改属性

javascript - 由 jquery 函数填充的文本框需要小数

javascript - 如何更改应用于几个元素中的跨度的类?

html - 在多个 SVG 元素中管理相同的字体大小

javascript - Nightwatch - 在导航栏中抓取嵌套按钮