AngularJS 1.2 ng-repeat animation on page load

浪子不回头ぞ 提交于 2019-12-24 02:20:29

问题


Staggering Animations are great! But I don't get it to work without user interaction.

There is a total normal ng-repeat list:

<div ng-controller="controller">    
    <div class="category" ng-repeat="category in categories">
      {{category}}
    </div>
</div>

Defined in a controller:

var controller = function($scope) {
  $scope.categories = ['12345', '6789', '9876', '54321'];
};

And a few CSS Rules for animation:

.category.ng-enter {
  -webkit-transition: 2s linear all;
  transition: 2s linear all;
  opacity:0;
}
.category.ng-enter-stagger {
  -webkit-transition-delay: 1s;
  transition-delay: 1s;
}
.category.ng-enter.ng-enter-active {
  /* standard transition styles */
  opacity:1;
}

But on page load its displayed without animation. I inserted a button for replacing the categories array with random numbers and it fades in just fine.

What do I have to do to get the animation to work, when the user visits the page the first time?

Demo is here: http://plnkr.co/edit/3zXENPbYA3cxJQ3Pyb34?p=preview

I found some answers that hacking around with $timeout() but I get an animation only on the first item. And it don't feel well.


回答1:


It's by design that the animation is disabled while bootstraping, see: #5130.

There is a workaround (a dirty hack) provided in a comment by lioli to enable animation on page load.

Put this line at the beginning of your controller (do not forget to inject the $rootElement).

$rootElement.data("$$ngAnimateState").running = false;

Example Plunker: http://plnkr.co/edit/9ZZ3JBOCaRJ41ssczX6l?p=preview

For the issue that you get an animation only on the first item. It's been reported that this is a bug that happens only with a minified version of angular-animate i.e. angular-animate.min.js.

In the plunker above, I've changed to an unminified angular-animate.js and it seems to work fine.

For more detail of the issue, see: #8297 and #7547




回答2:


Another option besides @runTarm's answer would be to fill the data in after the intial load. Something as simple as:

$scope.items = [];

var items = 'abcdefghijklmnopqrstuvwxyz'.split("");

$timeout(function() {
  $scope.items = items;  
}, 0);

Modified plunkr example



来源:https://stackoverflow.com/questions/25119037/angularjs-1-2-ng-repeat-animation-on-page-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!