Scroll to bottom of div?

前端 未结 30 2723
日久生厌
日久生厌 2020-11-21 11:56

I am creating a chat using Ajax requests and I\'m trying to get messages div to scroll to the bottom without much luck.

I am wrapping everything in this div:



        
30条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-21 12:17

    Like you, I'm building a chat app and want the most recent message to scroll into view. This ultimately worked well for me:

    //get the div that contains all the messages
    let div = document.getElementById('message-container');
    
    //make the last element (a message) to scroll into view, smoothly!
    div.lastElementChild.scrollIntoView({ behavior: 'smooth' });
    

提交回复
热议问题