简体   繁体   English

将滚动条强制到底部jquery / css

[英]Forcing scrollbar to the bottom jquery/css

I have created a chatroom which elements are li . 我创建了一个聊天室,其中的元素是li I want it to always stick to the bottom. 我希望它始终坚持到底。 It works fine when number of chats are like 10-20 but after that it stops halfway. 当聊天次数大约为10-20时,它可以正常工作,但此后它会中途停止。 I think there is something wrong with the height but I can't figure it out here is my html : 我认为高度有问题,但我无法确定这是我的html:

<div id="chat-main">
  <ul id="chat-list">
    //I dynamically add <li>s using ajax
  </ul>
</div>
<div id="message-panel">
  <form class="form-inline" id="messageForm">
    <input id="message-value" type="text" autocomplete="off" maxlength="510"  placeholder="Type your message...">
  </form>
</div>

Here is my css: 这是我的CSS:

#chat-main {
  height: 84%;
  border: 2px solid #d8d7cf;
}

#chat-list {
  height: 100%;
  overflow-y: scroll;
}

#chat-main>ul {
  padding-left: 10px;
  list-style-type: none;
}

And this is the jquery code for scrolling: 这是滚动的jQuery代码:

 $("#chat-list").animate({ scrollTop: $(document).height() }, "slow")

I'm really stuck and don't know what is wrong. 我真的被卡住了,不知道出什么问题了。

Edit: here is my jsfiddle sorry for the chat objects I inserted alot so you can see what happens : 编辑:这是我的jsfiddle对不起我插入了很多聊天对象,以便您可以看到发生了什么:

http://jsfiddle.net/L8msx/19/ http://jsfiddle.net/L8msx/19/

Using document height doesn't seem logical, this will get the exact overflow of the chat : 使用文档高度似乎不合逻辑,这将获得聊天的确切溢出:

http://jsfiddle.net/1yLzkgw8/ http://jsfiddle.net/1yLzkgw8/

var chat = $("#chat-list"),
overflow = chat[0].scrollHeight-chat.height(); 

chat.animate({scrollTop: overflow}, 'slow');

When getting scrollHeight , plain JS is used so the DOM node itself is retrieved with [0] . 获取scrollHeight ,将使用纯JS,因此可以使用[0]检索DOM节点本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM