Scroll to bottom of div?

前端 未结 30 2548
日久生厌
日久生厌 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:24

    use :

    var element= $('element');
    var maxScrollTop = element[0].scrollHeight - element.outerHeight();
    element.scrollTop(maxScrollTop);
    

    or check scroll to bottom :

        var element = $(element);
        var maxScrollTop = element[0].scrollHeight - element.outerHeight();
        element.on('scroll', function() {
            if ( element.scrollTop() >= maxScrollTop ) {
                alert('scroll to bottom');
            }
        });
    

提交回复
热议问题