【问题标题】:Scroll div to bottom when recharged充电时将div滚动到底部
【发布时间】:2019-02-23 21:52:29
【问题描述】:

我有一个自动重新加载 div 内容的脚本。该代码有效,但是有一件事我无法解决:

第一次加载页面时,滚动条会移到 div 的底部(这很好),但是当您使用脚本更新时(从第二个开始),滚动条会保持向上。

<script>
  setInterval("my_function1();",5000);
  $("#interno_chat").scrollTop($("#interno_chat")[0].scrollHeight);
  function my_function1(){
    $('#interno_chat').load(window.location.href + ' #interno_chat');

  }
</script>

如果我将 scrollTop-scrollHeight 放在 my_function1 中,代码即使是第一次也不起作用。

谢谢

【问题讨论】:

    标签: javascript jquery html scroll


    【解决方案1】:
    <script>
      setInterval("my_function1();",5000);
      $("#interno_chat").scrollTop($("#interno_chat")[0].scrollHeight);
      function my_function1(){
        $('#interno_chat').load(window.location.href + ' #interno_chat', function() {
            $("#interno_chat").scrollTop($("#interno_chat")[0].scrollHeight);
        });        
      }
    </script>
    

    我怀疑问题在于您没有等待加载内容。

    http://api.jquery.com/load/

    【讨论】:

    • 我认为问题也出在...我已经尝试了您的代码,但没有,滚动条停留在 div 的顶部。你知道解决这个问题的另一种方法吗?
    猜你喜欢
    • 2016-12-27
    • 2021-11-12
    • 2018-03-19
    • 1970-01-01
    • 2012-07-13
    • 2010-09-21
    相关资源
    最近更新 更多