【问题标题】:Maintain the scroll position when prepending content to a list (AngularJS)将内容附加到列表时保持滚动位置(AngularJS)
【发布时间】:2015-12-06 19:44:19
【问题描述】:

我一直在尝试使用ng-repeat 将一些项目添加到可滚动容器内的列表中,最近的项目应该位于列表的顶部。如果在添加内容时容器的滚动条不在最顶部,我还需要保持滚动位置。

这是我的解决方案,但我仍有问题。 angular 渲染 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() {
      // Executed after the dom has finished rendering
      if ($container.scrollTop() !== 0) {
        $container.scrollTop(oScrollTop + ($topItem.length ? $topItem.position().top : 0) - oOffset);
      }
    });
  }, 1000);
});
.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>

【问题讨论】:

    标签: javascript jquery angularjs dom scroll


    【解决方案1】:

    我找到了this post 并将$timeout 更改为$scope.$$postDigest。现在它按预期工作了。

    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;
    
        $scope.$$postDigest(function() {
          // Executed after the dom has finished rendering
          if ($container.scrollTop() !== 0) {
            $container.scrollTop(oScrollTop + ($topItem.length ? $topItem.position().top : 0) - oOffset);
          }
        });
      }, 1000);
    });
    .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>

    【讨论】:

      猜你喜欢
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 2012-04-27
      • 2020-04-06
      • 1970-01-01
      相关资源
      最近更新 更多