Scroll to bottom of div?

前端 未结 30 2817
日久生厌
日久生厌 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
    2020-11-21 12:02

    Found this really helpful, thank you.

    For the Angular 1.X folks out there:

    angular.module('myApp').controller('myController', ['$scope', '$document',
      function($scope, $document) {
    
        var overflowScrollElement = $document[0].getElementById('your_overflow_scroll_div');
        overflowScrollElement[0].scrollTop = overflowScrollElement[0].scrollHeight;
    
      }
    ]);
    

    Just because the wrapping in jQuery elements versus HTML DOM elements gets a little confusing with angular.

    Also for a chat application, I found making this assignment after your chats were loaded to be useful, you also might need to slap on short timeout as well.

提交回复
热议问题