Peter J Peter J - 7 days ago 7
CSS Question

Display in Chat

To properly display a chat window, I ended with this code :

function baja_el_chat() {
var height = 0;
$('div#my-conversation .message').each(function(i, value){
height += parseInt($(this).height());
});

height += '';

$('div#my-conversation').animate({scrollTop: height});
}

Answer

I did a chat window from scratch a few weeks ago. The way the browser builds your stack of divs will make the most recent messages to be hidden below the window's limits as expected. The same happened to me so I built this Javascript/jQuery function:

function baja_el_chat() {
  var height = 0;
  $('div#my-conversation .message').each(function(i, value){
      height += parseInt($(this).height());
  });

  height += '';

  $('div#my-conversation').animate({scrollTop: height});
}

Basically what it does is to measure the height of the content based on the sum of all chat bubbles and then scroll that amount. I call this function every time the user hits enter or receives a new message.

Comments