Automatically scroll down chat div

后端 未结 11 2261
时光取名叫无心
时光取名叫无心 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 07:59

    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 -

提交回复
热议问题