简体   繁体   English

使用angularjs列出重新排序动画

[英]List reorder animation with angularjs

I'm working on a project in angularjs that has a list of objects which update in real time and will be reordered with each update. 我正在开发一个angularjs项目,该项目包含一个实时更新的对象列表,并将在每次更新时重新排序。 I want to animate these objects moving smoothly from their starting to their end positions, so for example when the list reorder is: 我希望动画这些对象从它们的起始位置平滑移动到最终位置,例如当列表重新排序时:

A         C
B   ->    A
C         B

A and B will each move down one spot, and C will move up two spots, twice as quickly. A和B将分别向下移动一个点,C将向上移动两个点,两倍的速度。 This is easily done when you are manipulating the DOM manually - if you are moving the list element by changing it's style.top, you just put 当您手动操作DOM时,这很容易实现 - 如果您通过更改它的style.top来移动列表元素,那么您只需要

transition-duration: 0.5s, 0.5s;
transition-property: top;

into the CSS for the element and it happens automatically. 进入元素的CSS,它会自动发生。 However, this trick won't work if you are using ngRepeat to display your list, because (as far as I can tell) angular actually recreates the DOM elements making up the list to do an update, rather than moving the DOM elements around. 但是,如果你使用ngRepeat显示列表,这个技巧将不起作用,因为(据我所知)angular实际上重新创建组成列表的DOM元素来进行更新,而不是移动DOM元素。

Unfortunately, I've found it really difficult to reproduce this functionality with angular animations. 不幸的是,我发现用角度动画重现这个功能真的很难。 The problem that I'm running into is that angular move animations seem to be unaware of each element's starting position. 我遇到的问题是角度移动动画似乎没有意识到每个元素的起始位置。 With the ngAnimate directive you can have angular automatically set a css class on your element when it moves, to simulate it fading in or out for example. 使用ngAnimate指令,您可以让角度在元素移动时自动设置css类,以模拟它淡入或淡出(例如)。 But you have no information on where the element used to be, so you can't move it smoothly from it's old position - it just gets teleported to the new spot, and you have to make it dance around there. 但是你没有关于这个元素曾经在哪里的信息,所以你不能顺利地从它的旧位置移动它 - 它只是被传送到新的位置,你必须让它在那里跳舞。 As far as I can tell, this is also true for javascript animations - angular teleports it into place and then passes it to your function, without any history information. 据我所知,javascript动画也是如此 - 角度将其传送到位,然后将其传递给您的函数,没有任何历史信息。

Is there a way within angular to have a smooth reordering animation as described above, without ditching the framework and handling the DOM manipulation yourself? 在角度内是否有一种方法可以获得如上所述的平滑重新排序动画,而不必放弃框架并自己处理DOM操作?

I think I've accomplished what you're looking for here: http://codepen.io/daleyjem/pen/xbZYpY 我想我已经完成了你在这里寻找的东西: http//codepen.io/daleyjem/pen/xbZYpY

By using track by , I'm able to keep the DOM elements from being recreated, and can then manipulate their position. 通过使用track by ,我能够保持DOM元素不被重新创建,然后可以操纵它们的位置。

<div ng-repeat="item in items | orderBy:sorter track by item.id" class="item" jd-script>
    {{ item.id }}: {{ item.last_name }}, {{ item.first_name }}
</div>

我想这就是你要找的东西: http//www.nganimate.org/angularjs/ng-repeat/move

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM