AngularJS - 将项目从一个容器动画到另一个容器

3
我有两个div,其中一个是空的,另一个有一些预定义的项目。点击其中一个列表中的项目,另一个列表将添加项目。
我想实现的是动画效果,使项目从一个列表移动到另一个列表(我希望项目从右向左移动)。我不太了解CSS动画或AngularJS动画。在jQuery中,我只需调用$().animate()来修改属性即可。
那么在AngularJS中,我该如何做相同的事情呢?
JS:
var app = angular.module('app', []);

app.controller('Ctrl', function ($scope) {

  $scope.selectedItems = [];
  $scope.items = ['a', 'b', 'c'];

});

HTML:

<div ng-controller="Ctrl">

<div class='container'> 
  <div class='inline selected-container' >
    <div ng-repeat='selected in selectedItems track by $index'> <!-- I would like items to appear here after items from other container have finished moving -->
      {{selected}}
    </div>
  </div>

  <div class="inline">
    <!-- I would like to push these items left on click -->
    <div class='inline box' ng-repeat="item in items" ng-click="selectedItems.push(item);"> 
      {{item}}
    </div>
  </div>
</div>

</div>

这是我目前的进展链接:

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


1
我认为最好的方法是在物品仍在一个列表中时对其进行动画处理,使其看起来加入了另一个列表,然后在动画完成后通过编程切换列表。但我希望有更酷的方法。 - jraede
请阅读此内容:https://dev59.com/YGUp5IYBdhLWcg3w3KXe#15012542 - Eliran Malka
@EliranMalka,我已经阅读过那篇文章,但那并没有解决当前问题。 - SoluableNonagon
没错,我只是觉得这会有助于阐明事情。并不是我的意思要无礼。 - Eliran Malka
1个回答

1

我在这个页面上读到了一个关于ng-animate的不错的解释:http://www.jvandemo.com/how-to-create-cool-animations-with-angularjs-1-2-and-animate-css/

基本上,你需要在CSS中定义动画,然后将ng-enter和ng-leave附加到你想要实现这些动画的类上。

/* Define your Animation , or just download animate.css which has all these defined*/ 
@-webkit-keyframes fadeOutLeft {
  0% {
    opacity: 1;
    -webkit-transform: translateX(0);
    transform: translateX(0);
  }

  100% {
    opacity: 0;
    -webkit-transform: translateX(-20px);
    transform: translateX(-20px);
  }
}

@keyframes fadeOutLeft {
  0% {
    opacity: 1;
    -webkit-transform: translateX(0);
    -ms-transform: translateX(0);
    transform: translateX(0);
  }
  100% {
    opacity: 0;
    -webkit-transform: translateX(-20px);
    -ms-transform: translateX(-20px);
    transform: translateX(-20px);
  }
}
.fadeOutLeft {
  -webkit-animation-name: fadeOutLeft;
  animation-name: fadeOutLeft;
}

你需要在CSS中将动画附加到你的元素:

/* Apply your animation, which will run on ng-repeat, ng-hide, ng-show, etc */
.item {
   /* this is the element you're animating  */
}
.item.ng-enter {  /* attach ng-enter */
    -webkit-animation: fadeInLeft 1s;
    -moz-animation: fadeInLeft 1s; 
    -ms-animation: fadeInLeft 1s;
    animation: fadeInLeft 1s;
}
.item.ng-leave { /* attach ng-leave */
    -webkit-animation: fadeOutLeft 1s;  /*  */
    -moz-animation: fadeOutLeft 1s;
    -ms-animation: fadeOutLeft 1s; 
    animation: fadeOutLeft 1s;
}

Updated:
http://plnkr.co/edit/TZPmsCcR1xMcPW4eIEf3?p=preview


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接