Scroll to bottom of div?

前端 未结 30 2728
日久生厌
日久生厌 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条回答
  •  醉话见心
    2020-11-21 12:19

    Just as a bonus snippet. I'm using angular and was trying to scroll a message thread to the bottom when a user selected different conversations with users. In order to make sure that the scroll works after the new data had been loaded into the div with the ng-repeat for messages, just wrap the scroll snippet in a timeout.

    $timeout(function(){
        var messageThread = document.getElementById('message-thread-div-id');
        messageThread.scrollTop = messageThread.scrollHeight;
    },0)
    

    That will make sure that the scroll event is fired after the data has been inserted into the DOM.

提交回复
热议问题