Automatically scroll down chat div

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

    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>

    0 讨论(0)
  • 2020-12-05 08:10

    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.

    0 讨论(0)
  • 2020-12-05 08:10

    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>

    0 讨论(0)
  • 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
        }
    
    0 讨论(0)
  • 2020-12-05 08:13
        var l = document.getElementsByClassName("chatMessages").length;
        document.getElementsByClassName("chatMessages")[l-1].scrollIntoView();
    

    this should work

    0 讨论(0)
提交回复
热议问题