Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically scroll down chat div

I have this code, to load chat

function getMessages(letter) {   var div = $('#messages');   $.get('msg_show.php', function (data) {     div.html(data);   }); }  setInterval(getMessages, 100); 

What I have to add, to automatically scroll down #messages div on page load, and again with every new chat post?

This doesn't work:

function getMessages(letter) {   var div = $('#messages');   $.get('msg_show.php', function (data) {     div.html(data);     $('#messages').scrollTop($('#messages')[0].scrollHeight);   }); }  setInterval(getMessages, 100); 
like image 765
Adam Výborný Avatar asked Aug 26 '14 12:08

Adam Výborný


People also ask

How do I make Div scroll automatically?

Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.

How do I automatically scroll down in HTML?

scrollTop = 1000; ). The second is setting the link to point to a specific id in the page e.g. Then if in your target page you'll have that ID the page will be scrolled automatically.

What is scrollTop in Javascript?

The Element. scrollTop property gets or sets the number of pixels that an element's content is scrolled vertically. An element's scrollTop value is a measurement of the distance from the element's top to its topmost visible content.

What is scrolling the chat screen?

What does 'scrolling the chat screen' in an internet chat room mean, and is it good or bad netiquette? Looking further down the screen (good netiquette). Scrolling down to follow the conversation as it develops (bad netiquette).


1 Answers

Let's review a few useful concepts about scrolling first:

  • scrollHeight: total container size.
  • scrollTop: amount of scroll user has done.
  • clientHeight: amount of container a user sees.

When should you scroll?

  • User has loaded messages for the first time.
  • New messages have arrived and you are at the bottom of the scroll (you don't want to force scroll when the user is scrolling up to read previous messages).

Programmatically that is:

if (firstTime) {   container.scrollTop = container.scrollHeight;   firstTime = false; } else if (container.scrollTop + container.clientHeight === container.scrollHeight) {   container.scrollTop = container.scrollHeight; } 

Full chat simulator (with JavaScript):

https://jsfiddle.net/apvtL9xa/

const messages = document.getElementById('messages');    function appendMessage() {  	const message = document.getElementsByClassName('message')[0];    const newMessage = message.cloneNode(true);    messages.appendChild(newMessage);  }    function getMessages() {  	// Prior to getting your messages.    shouldScroll = messages.scrollTop + messages.clientHeight === messages.scrollHeight;    /*     * Get your messages, we'll just simulate it by appending a new one syncronously.     */    appendMessage();    // After getting your messages.    if (!shouldScroll) {      scrollToBottom();    }  }    function scrollToBottom() {    messages.scrollTop = messages.scrollHeight;  }    scrollToBottom();    setInterval(getMessages, 100);
#messages {    height: 200px;    overflow-y: auto;  }
<div id="messages">    <div class="message">      Hello world    </div>  </div>
like image 173
zurfyx Avatar answered Sep 20 '22 10:09

zurfyx