0

I need to make a certain div contents to simulate a scroll, when new data is added, kind of like facebook chat. How do I go about this? I'm using jQuery.

Here's a markup sample :

<div id="chat-messages">
   <div class="msg">John Doe says : Hi!</div>
</div>
PeeHaa
  • 69,318
  • 57
  • 185
  • 258
yoda
  • 10,512
  • 19
  • 62
  • 92

3 Answers3

3

Solutions found similar to what you are describing:

There are some other solutions here:

Community
  • 1
  • 1
JSuar
  • 20,828
  • 4
  • 42
  • 81
3

First, you need to put a fixed height (height: 400px) on the container div (chat-messages) and a scroll(overflow-y:scroll) for vertical content to make the scroll appear.

Next, when a new message is posted, you need to scroll down using javascript. For example:

$(".chat-messages").attr({ scrollTop: $(".chat-messages").attr("scrollHeight") });

Or animate the scroll:

$(".chat-messages").animate({ scrollTop: $("chat-messages").attr("scrollHeight") }, 1000);
Stelian Matei
  • 11,163
  • 2
  • 24
  • 29
1

This will append new content at the bottom of a div. I guess that's what you want.

$('#chat-messages').append(newdiv);

But I think you need to do a bit of background reading. Check this out.

Jivings
  • 22,276
  • 6
  • 54
  • 98