I am trying to add some elements to a list in a scrollable container using ng-repeat , and the recent one at the top of the list. I should also maintain the scroll position if the scrollbar of the container is not at the very top when adding content.
Here is my solution, but I still have a problem. There is always a flicker after angular selects the added elements in dom.
var myApp = angular.module('myApp', []); myApp.controller('MainCtrl', function($scope, $interval, $timeout) { $scope.items = []; $interval(function() { var item = { id: Math.random(), text: (new Date()).toString() }; $scope.items.unshift.apply($scope.items, [item]); var $container = $('.stream-container'); var $topItem = $('.item:first'); var oScrollTop = $container.scrollTop(); var oOffset = $topItem.length ? $topItem.position().top : 0; $timeout(function() {
.stream-container { overflow-y: scroll; overflow-x: none; height: 100px; position: relative; }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <body ng-app="myApp"> <div class="stream-container" ng-controller="MainCtrl"> <div class="stream"> <div class="item" ng-repeat="item in items track by item.id">{{ item.text }}</div> </div> </div> </body>
source share