2016-05-18 41 views
-4
<div class="container"> 
    <label ng-repaet"x in items">{{x}}</label> 
</div> 

我想在容器溢出时自动将容器div的滚动条放在底部,并将标签项添加到底部。如何在div中添加新内容时自动向下滚动到div的底部?

的DIV容器的CSS类是:

.container{ 
    position:relative; 
    display:block; 
    overflow-y:scroll; 
    overflow-wrap:break-word; 
    height:300px; 
} 

,也是我有风格我的滚动条如下:

::-webkit-scrollbar{ 
    width:6px; 
} 
::-webkit-scrollbar-track{ 
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); 
    border-radius: 0px; 
} 
::-webkit-scrollbar-thumb{ 
    border-radius: 0px; 
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.5); 
} 
+1

http://stackoverflow.com/questions/12790854/angular-directive-to-scroll-to-a-given-item试试这个 – byteC0de

+0

它不适合我@Jimbrooism – Krisalay

+0

我已经检查过,它didn没有工作。 @AlexLogan – Krisalay

回答

0

您可以在指令中的链接功能添加$scope.$watch像这样:

function dynamicContainer() { 
    return { 
     restrict: 'AE', 
     template: '<div class="container container-dynamic"><div ng-repeat="item in data track by $index">{{item}}</div></div>', 
     scope: { 
      data: '=data' 
     }, 
     replace: true, 
     link: ($scope, $el, $attrs) => { 
      $scope.$watch(() => { 
       return $el[0].childNodes.length; 
      }, (newValue, oldValue) => { 
       $el.animate({ 
        scrollTop: $el[0].scrollHeight 
       }); 

       if (newValue !== oldValue) { 
        $el.animate({ 
         scrollTop: $el[0].scrollHeight 
        }); 
       } 
      }); 
     } 
    }; 
} 

这是你的pen