【发布时间】:2019-11-23 09:40:57
【问题描述】:
这是一个绑定到 NgRx 存储中状态的简单 div。
<div class="dot"
[style.width.px]="size$ | async"
[style.height.px]="size$ | async"
[style.backgroundColor]="color$ | async"
[style.left.px]="x$ | async"
[style.top.px]="y$ | async"
(transitionstart)="transitionStart()"
(transitionend)="transitionEnd()"></div>
点状态变化由 CSS 过渡动画。
.dot {
border-radius: 50%;
position: absolute;
$moveTime: 500ms;
$sizeChangeTime: 400ms;
$colorChangeTime: 900ms;
transition:
top $moveTime, left $moveTime,
background-color $colorChangeTime,
width $sizeChangeTime, height $sizeChangeTime;
}
我有一个推送点更新的后端(位置、颜色和大小)。我将这些更新映射到 NgRx 操作上。
export class AppComponent implements OnInit {
...
constructor(private store: Store<AppState>, private backend: BackendService) {}
ngOnInit(): void {
...
this.backend.update$.subscribe(({ type, value }) => {
// TODO: trigger new NgRx action when all animations ended
if (type === 'position') {
const { x, y } = value;
this.store.dispatch(move({ x, y }));
} else if (type === 'color') {
this.store.dispatch(changeColor({ color: value }));
} else if (type === 'size') {
this.store.dispatch(changeSize({ size: value }));
}
});
}
}
问题是来自后端的新更改有时会在动画结束之前出现。
我的目标是延迟更新存储中的状态(暂停触发新的 NgRx 操作),直到所有转换结束。我们可以轻松处理这一时刻,因为 chrome 已经支持 transitionstart 事件。
间距取决于过渡持续时间。
这是可运行的应用程序https://stackblitz.com/edit/angular-qlpr2g 和repo https://github.com/cwayfinder/pausable-ngrx。
【问题讨论】:
-
似乎您已经在使用
transitionstart和transitionend。是积累的问题吗?你可以使用一个简单的计数器,它在转换开始时增长,在转换结束时减少,这样你的下一个事件只会在计数器为 0 时触发? -
问题在于积累本身。我需要创建一些缓冲区左右。
-
如果这是所有动画的单个组件,您可以添加一个类成员。如果这需要在组件之间进行协调,您可以在商店中添加一个 state 属性来跟踪所有组件。
-
这是一个如何管理 RxJS 动作流以使其可暂停的问题。
-
我只是好奇你是如何运行动画的?您是否将某个类添加到动画元素?
标签: javascript angular rxjs css-transitions ngrx