Automatically scroll down chat div

后端 未结 11 2259
时光取名叫无心
时光取名叫无心 2020-12-05 07:43

I have this code, to load chat

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


        
11条回答
  •  臣服心动
    2020-12-05 08:12

    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
        }
    

提交回复
热议问题