默认滚动条位置

Default scrollbar position

本文关键字:位置 滚动条 默认      更新时间:2023-09-26

我正在构建一个聊天应用程序,在页面加载时,我需要将滚动条放在聊天窗口(最后一条消息)的底部。

我该如何做到这一点(使用jQuery或其他方式)?

这是聊天窗口div:

<div id="ccwindow">
      <div class="bubble me">Hey!</div>
      <div class="bubble you">Hello</div>
      <div class="bubble me">I saw your post. Want to buy the book. Are you an XYZ University?</div>
      <div class="bubble you">Yes, sophomore year.</div>
      <div class="bubble me">Great!</div>
      <div class="bubble you">I'll buy it. Can you get it to school tomorrow?</div>
      <div class="bubble me">Sure. See you tomorrow!</div>
 </div>

开始:http://jsfiddle.net/vtep7Lf1/

$("document").ready(function() {
  $("#ccwindow").animate({ scrollTop: $("#ccwindow").height() }, "slow");
  return false;
});

您也可以使用这个:

$(window).load(function() {
    $("#ccwindow").animate({ scrollTop: $(document).height() - $(window).height() }, 1000);
});

注:

使用.load而不是.ready将考虑图像。