AngularMaterial拖放在CSS转换结束后调用回调的问题
创始人
2024-10-28 11:01:31
0

可以使用animationEnd事件来检测CSS过渡的结束,然后在回调函数中执行相关操作。以下是示例代码:

HTML:


  Drag me!

TS:

import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { CdkDragEnd } from '@angular/cdk/drag-drop';

@Component({
  selector: 'app-drag-example',
  templateUrl: './drag-example.component.html',
  styleUrls: ['./drag-example.component.scss']
})
export class DragExampleComponent implements OnInit {

  @ViewChild('dragItem') dragItem: ElementRef;

  ngOnInit() {
    const item = this.dragItem.nativeElement;
    if (!item) {
      return;
    }

    item.addEventListener('animationend', (e: AnimationEvent) => {
      if (e.animationName === 'cdk-drag-placeholder') {
        // Place your code here, the animation is finished
      }
    });
  }

  onDragEnded(event: CdkDragEnd) {
    const item = event.source.element.nativeElement;

    // Reset the animation
    item.classList.add('cdk-drag-placeholder');
    item.style.transform = 'translate3d(0, 0, 0)';
    item.addEventListener('animationend', (e: AnimationEvent) => {
      if (e.animationName === 'cdk-drag-placeholder') {
        // Place your code here, the animation is finished
      }
    });
  }
}

请注意,在HTML中我们使用了“cdkDragReleased”代替默认事件,以在释放拖动项时调用“onDragEnded”函数。在函数中,我们需要重置动画并添加“animationend”事件侦听器来检测过渡的结束。同样,在ngOnInit中,我们检测元素上的“animationend”事件以处理完成动画时的操作。

相关内容

热门资讯

透视模块!wepoker有辅助... 透视模块!wepoker有辅助器吗(WePoKer购买)竟然是有辅助方法(哔哩哔哩)1、wepoke...
透视透视!微乐小程序免费黑科技... 透视透视!微乐小程序免费黑科技,微乐小程序破解(透视)原来是有辅助app(哔哩哔哩)透视方法中分为三...
透视学习!wepoker辅助软... 透视学习!wepoker辅助软件视频(WePoKer有挂)总是有辅助工具(哔哩哔哩)1、完成wepo...
据统计!微乐小程序黑科技,微乐... 据统计!微乐小程序黑科技,微乐自建房插件免费软件(透视)切实存在有辅助神器(哔哩哔哩)1、上手简单,...
透视阶段!聚星ai辅助工具激活... 透视阶段!聚星ai辅助工具激活码(WPK下载)一贯是真的有辅助攻略(哔哩哔哩)1、首先打开聚星ai辅...
透视新版!微乐小程序黑科技,微... 透视新版!微乐小程序黑科技,微信小程序微乐挂如何获取(透视)竟然真的是有辅助工具(哔哩哔哩)1、许多...
透视窍要!wepoker有什么... 透视窍要!wepoker有什么规律(HHpoker辅助)好像存在有辅助神器(哔哩哔哩);1、进入游戏...
据报道!微乐小程序黑科技,微乐... 据报道!微乐小程序黑科技,微乐江西小程序辅助器(透视)其实存在有辅助脚本(哔哩哔哩)1、据报道!微乐...
透视手段!德州透视是真的假的(... 透视手段!德州透视是真的假的(WePoKer数据)确实有辅助工具(哔哩哔哩)德州透视是真的假的透视方...
随着!微信微乐游戏辅助脚本,微... 随着!微信微乐游戏辅助脚本,微乐小程序自建房辅助器下载(透视)果然存在有辅助工具(哔哩哔哩)1、很好...