I have this code, to load chat
function getMessages(letter) {
var div = $(\'#messages\');
$.get(\'msg_show.php\', function (data) {
div.html(data);
Let's review a few useful concepts about scrolling first:
When should you scroll?
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>
What you need to do is divide it into two divs. One with overflow set to scroll, and an inner one to hold the text so you can get it's outersize.
<div id="chatdiv">
<div id="textdiv"/>
</div>
textdiv.html("");
$.each(chatMessages, function (i, e) {
textdiv.append("<span>" + e + "</span><br/>");
});
chatdiv.scrollTop(textdiv.outerHeight());
You can check out a jsfiddle here: http://jsfiddle.net/xj5c3jcn/1/
Obviously you don't want to rebuild the whole text div each time, so take that with a grain of salt - just an example.
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>
if you just scrollheight it will make a problem when user will want to see his previous message. so you need to make something that when new message come only then the code. use jquery latest version. 1.here I checked the height before message loaded. 2. again check the new height. 3. if the height is different only that time it will scroll otherwise it will not scroll. 4. not in the if condition you can put any ringtone or any other feature that you need. that will play when new message will come. thanks
var oldscrollHeight = $("#messages").prop("scrollHeight");
$.get('msg_show.php', function(data) {
div.html(data);
var newscrollHeight = $("#messages").prop("scrollHeight"); //Scroll height after the request
if (newscrollHeight > oldscrollHeight) {
$("#messages").animate({
scrollTop: newscrollHeight
}, 'normal'); //Autoscroll to bottom of div
}
var l = document.getElementsByClassName("chatMessages").length;
document.getElementsByClassName("chatMessages")[l-1].scrollIntoView();
this should work