Scroll to bottom in chat box in angularjs

前端 未结 2 1857
Happy的楠姐
Happy的楠姐 2020-12-30 21:07

I am trying to automatically scroll to bottom whenever there is a new message.

My code moves the scrollbar but it does not take it to exact bottom. Kindly help. Her

2条回答
  •  忘掉有多难
    2020-12-30 21:24

    You can create a directive for this:

    .directive('scrollBottom', function () {
      return {
        scope: {
          scrollBottom: "="
        },
        link: function (scope, element) {
          scope.$watchCollection('scrollBottom', function (newValue) {
            if (newValue)
            {
              $(element).scrollTop($(element)[0].scrollHeight);
            }
          });
        }
      }
    })
    

    http://plnkr.co/edit/H6tFjw1590jHT28Uihcx?p=preview

    BTW: avoid DOM manipulation inside controllers (use directives instead).

提交回复
热议问题