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

时间:2014-03-21 16:06:13

标签: css angularjs css-animations angularjs-animation

我有两个div,一个开始为空,另一个有一些预定义的项目。单击一个列表中的项目时,另一个列表会添加添加到其中的项目。

我想要实现的是将一个列表中的项目动画显示为物理移动到另一个列表。 (我希望物品从右向左移动)。我不太了解CSS动画或AngularJS动画。在jQuery中,我可以在div id上调用$()。animate()来修改属性。

hoping to achieve

我如何在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 个答案:

答案 0 :(得分: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;
}

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