作者:覃维欢 | 来源:互联网 | 2023-02-04 10:21
Vue用户很容易实现这样的项目随机播放动画,请参阅他们的官方文档:
我搜索了很多,但找不到Angular用户的解决方案.ngFor
在洗牌时似乎切换项目内容而不是移动项目.
这是我的演示:http://embed.plnkr.co/3IcKcC/
单击shift时,您应该看到项目移动动画,这要归功于li {transform: all 1s;}
.但是当你洗牌时,没有动画.所以我在这里要求一个解决方案.
1> yurzui..:
这里是简单实现这样的功能Plunker Example
1)构建指令
@Directive({
selector: '[transition-group-item]'
})
export class TransitionGroupItemDirective {
prevPos: any;
newPos: any;
el: HTMLElement;
moved: boolean;
moveCallback: any;
constructor(elRef: ElementRef) {
this.el = elRef.nativeElement;
}
}
@Component({
selector: '[transition-group]',
template: ''
})
export class TransitionGroupComponent {
@Input('transition-group') class;
@ContentChildren(TransitionGroupItemDirective) items: QueryList;
ngAfterContentInit() {
this.refreshPosition('prevPos');
this.items.changes.subscribe(items => {
items.forEach(item => {
item.prevPos = item.newPos || item.prevPos;
});
items.forEach(this.runCallback);
this.refreshPosition('newPos');
items.forEach(this.applyTranslation);
// force reflow to put everything in position
const offSet = document.body.offsetHeight;
this.items.forEach(this.runTransition.bind(this));
})
}
runCallback(item: TransitionGroupItemDirective) {
if(item.moveCallback) {
item.moveCallback();
}
}
runTransition(item: TransitionGroupItemDirective) {
if (!item.moved) {
return;
}
const cssClass = this.class + '-move';
let el = item.el;
let style: any = el.style;
el.classList.add(cssClass);
style.transform = style.WebkitTransform = style.transitiOnDuration= '';
el.addEventListener('transitionend', item.moveCallback = (e: any) => {
if (!e || /transform$/.test(e.propertyName)) {
el.removeEventListener('transitionend', item.moveCallback);
item.moveCallback = null;
el.classList.remove(cssClass);
}
});
}
refreshPosition(prop: string) {
this.items.forEach(item => {
item[prop] = item.el.getBoundingClientRect();
});
}
applyTranslation(item: TransitionGroupItemDirective) {
item.moved = false;
const dx = item.prevPos.left - item.newPos.left;
const dy = item.prevPos.top - item.newPos.top;
if (dx || dy) {
item.moved = true;
let style: any = item.el.style;
style.transform = style.WebkitTransform = 'translate(' + dx + 'px,' + dy + 'px)';
style.transitiOnDuration= '0s';
}
}
}
2)使用如下