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

时间:2017-07-31 12:02:41

标签: 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>

3 个答案:

答案 0 :(得分:0)

更改

@Multipart
@POST("AccountService/MultipartProfileImageUpload")Call<String> 
updateProfilePic(@Body UserIdCredentials 
userIdCredentials,@Part MultipartBody.Part file);

@Multipart
@POST("AccountService/MultipartProfileImageUpload")Call<String> 
updateProfilePic(@part("params") UserIdCredentials 
userIdCredentials,@Part MultipartBody.Part file);

答案 1 :(得分:0)

您可以尝试以下代码:

以下是演示:https://output.jsbin.com/vobomafehi

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script>
    $(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')[0].scrollHeight);
    });
  </script>
  <style>
    #content{
      max-height:250px;
      overflow-y:auto;
    }
  </style>
</head>
<body>
<div id="content"></div>
</body>
</html>

答案 2 :(得分:0)

问题是$('#content').scrollHeight未定义。您必须像这样访问它: $('#content')[0].scrollHeight

小提琴:https://jsfiddle.net/sr8fnv4k/

为什么呢? 因为有些属性没有转移到jQuery。在这种情况下,您需要访问原始的HTML-Tag属性。