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
  • 71,436
  • 58
  • 190
  • 262
yoda
  • 10,834
  • 19
  • 64
  • 92

3 Answers3

3

Solutions found similar to what you are describing:

There are some other solutions here:

Community
  • 1
  • 1
JSuar
  • 21,056
  • 4
  • 39
  • 83
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,553
  • 2
  • 25
  • 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,834
  • 6
  • 60
  • 101