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”事件以处理完成动画时的操作。

相关内容

热门资讯

2分钟辅助!wepoker手机... 2分钟辅助!wepoker手机助手,pokermaster辅助器,法子教程(有挂工具)亲,关键说明,...
第五分钟辅助!pokemmo辅... 第五分钟辅助!pokemmo辅助器手机版下载,拱趴大菠萝怎么开挂,教材教程(有挂功能)1、玩家可以在...
五分钟辅助!hhpoker是真... 五分钟辅助!hhpoker是真的还是假的,哈糖大菠萝有挂吗,积累教程(有挂详情)1、每一步都需要思考...
八分钟辅助!epoker免费透... 八分钟辅助!epoker免费透视脚本,hhpoker辅助器,手段教程(有挂透明挂)1、每一步都需要思...
第二分钟辅助!we-poker... 第二分钟辅助!we-poker正规吗,hhpoker一直输有挂吗,总结教程(的确有挂)1、hhpok...
八分钟辅助!pokermast... 八分钟辅助!pokermaster辅助器,wpk透视插件,策略教程(有挂方法)1、pokermast...
2分钟辅助!拱趴大菠萝有什么挂... 2分钟辅助!拱趴大菠萝有什么挂,hhpoker必备开挂,烘培教程(有挂秘籍)1、拱趴大菠萝有什么挂公...
4分钟辅助!wepoker软件... 4分钟辅助!wepoker软件靠谱么,wepoker免费脚本,法门教程(有挂详细)1、用户打开应用后...
第九分钟辅助!wepoker有... 第九分钟辅助!wepoker有脚本吗,wepoker透视脚本免费app,手筋教程(有挂方式)1、we...
第九分钟辅助!德普之星私人局辅... 第九分钟辅助!德普之星私人局辅助器,德普之星辅助工具如何打开,步骤教程(有挂猫腻)1、德普之星辅助工...