I have this code, to load chat
function getMessages(letter) {
var div = $(\'#messages\');
$.get(\'msg_show.php\', function (data) {
div.html(data);
I found out this very simple method while experimenting: set the scrollTo
to the height of the div.
var myDiv = document.getElementById("myDiv");
window.scrollTo(0, myDiv.innerHeight);
As I am not master in js but I myself write code which check if user is at bottom. If user is at bottom then chat page will automatically scroll with new message. and if user scroll up then page will not auto scroll to bottom..
JS code -
var checkbottom;
jQuery(function($) {
$('.chat_screen').on('scroll', function() {
var check = $(this).scrollTop() + $(this).innerHeight() >= $(this)
[0].scrollHeight;
if(check) {
checkbottom = "bottom";
}
else {
checkbottom = "nobottom";
}
})
});
window.setInterval(function(){
if (checkbottom=="bottom") {
var objDiv = document.getElementById("chat_con");
objDiv.scrollTop = objDiv.scrollHeight;
}
}, 500);
html code -
<div id="chat_con" class="chat_screen">
</div>
after append in JQuery,just add this line
<script>
$("#messages").animate({ scrollTop: 20000000 }, "slow");
</script>
to scroll till particular element from the message box top checkout the following demo:
https://jsfiddle.net/6smajv0t/
function scrollToBottom(){
const messages = document.getElementById('messages');
const messagesid = document.getElementById('messagesid');
messages.scrollTop = messagesid.offsetTop - 10;
}
scrollToBottom();
setInterval(scrollToBottom, 1000);
#messages {
height: 200px;
overflow-y: auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="messages">
<div class="message">
Hello world1
</div>
<div class="message">
Hello world2
</div>
<div class="message">
Hello world3
</div>
<div class="message">
Hello world4
</div>
<div class="message">
Hello world5
</div>
<div class="message">
Hello world7
</div>
<div class="message">
Hello world8
</div>
<div class="message">
Hello world9
</div>
<div class="message" >
Hello world10
</div>
<div class="message">
Hello world11
</div>
<div class="message">
Hello world12
</div>
<div class="message">
Hello world13
</div>
<div class="message">
Hello world14
</div>
<div class="message">
Hello world15
</div>
<div class="message" id="messagesid">
Hello world16 here
</div>
<div class="message">
Hello world17
</div>
<div class="message">
Hello world18
</div>
<div class="message">
Hello world19
</div>
<div class="message">
Hello world20
</div>
<div class="message">
Hello world21
</div>
<div class="message">
Hello world22
</div>
<div class="message">
Hello world23
</div>
<div class="message">
Hello world24
</div>
<div class="message">
Hello world25
</div>
<div class="message">
Hello world26
</div>
<div class="message">
Hello world27
</div>
<div class="message">
Hello world28
</div>
<div class="message">
Hello world29
</div>
<div class="message">
Hello world30
</div>
</div>
It's hard to tell without knowing the HTML code, but I'd assume your div doesn't have a height set and/or doesn't allow overflow (e.g. through CSS height: 200px; overflow: auto
).
I've made a working sample on jsfiddle: http://jsfiddle.net/hu4zqq4x/
I created some dummy HTML markup inside a div that is a) overflowing and b) has a set height. Scrolling is done through calling the function
function getMessages(letter) {
var div = $("#messages");
div.scrollTop(div.prop('scrollHeight'));
}
, in this case once on 'documentReady'.
prop('scrollHeight')
will access the value of the property on the first element in the set of matched elements.
Dont have to mix jquery and javascript. Use like this,
function getMessages(letter) {
var message=$('#messages');
$.get('msg_show.php', function(data) {
message.html(data);
message.scrollTop(message[0].scrollHeight);
});
}
setInterval(function() {
getMessages("letter");
}, 100)
Put the scrollTop()
inside get()
method.
Also you missed a parameter in the getMessage
method call..