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

相关内容

热门资讯

4分钟辅助!aapoker a... 4分钟辅助!aapoker ai插件,hhpoker是真的假的,烘培教程(有挂助手)1、实时hhpo...
九分钟辅助!pokemmo手机... 九分钟辅助!pokemmo手机版修改器,大菠萝手游辅助,诀窍教程(了解有挂)1、pokemmo手机版...
6分钟辅助!wepoker插件... 您好,wepoker插件程序激活码这款游戏可以开挂的,确实是有挂的,需要了解加去威信【1367043...
5分钟辅助!pokemmo脚本... 5分钟辅助!pokemmo脚本辅助器,poker红龙辅助,烘培教程(有挂攻略)1、用户打开应用后不用...
三分钟辅助!aapoker安装... 三分钟辅助!aapoker安装包怎么使用,aa poker辅助,经验教程(有挂教程)1、实时aapo...
六分钟辅助!德普之星app安卓... 六分钟辅助!德普之星app安卓版破解版,hhpoker到底可以作弊吗,资料教程(有挂秘籍)1、打开软...
6分钟辅助!hhpoker辅助... 6分钟辅助!hhpoker辅助挂是真的吗,拱趴大菠萝怎么开挂,烘培教程(有挂助手)1、用户打开应用后...
三分钟辅助!德州局怎么透视,p... 三分钟辅助!德州局怎么透视,pokermaster脚本,练习教程(有挂解密)1、许多玩家不知道德州局...
第3分钟辅助!德州透视hhpo... 第3分钟辅助!德州透视hhpoker,wepoker破解是真的还是假的,指南书教程(讲解有挂)1、首...
第2分钟辅助!pokemmo脚... 第2分钟辅助!pokemmo脚本辅助,aapoker怎么设置抽水,资料教程(有挂工具)1、首先打开a...