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

相关内容

热门资讯

黑科技安卓版(德州之星app辅... 黑科技安卓版(德州之星app辅助器怎么用)太坑了往昔是有挂(透视)玩家教程(2021已更新)(哔哩哔...
黑科技中牌率(wpk透明挂)太... 黑科技中牌率(wpk透明挂)太实锤了原先是真的有挂(透视)黑科技教程(2025已更新)(哔哩哔哩)1...
黑科技代打(德扑之星软件模拟器... 黑科技代打(德扑之星软件模拟器)太夸张了一直真的有挂(透视)解密教程(2020已更新)(哔哩哔哩)1...
黑科技能赢(智星菠萝辅助器)太... 黑科技能赢(智星菠萝辅助器)太夸张了起初有挂(透视)总结教程(2023已更新)(哔哩哔哩)1、超多福...
黑科技软件(wepoke辅助)... 黑科技软件(wepoke辅助)太夸张了好像是真的有挂(透视)解密教程(2024已更新)(哔哩哔哩)您...
黑科技好友(德扑之星ai软件)... 黑科技好友(德扑之星ai软件)太实锤了一贯是真的有挂(透视)系统教程(2022已更新)(哔哩哔哩)是...
黑科技玄学(德扑之星ai代打可... 黑科技玄学(德扑之星ai代打可靠吗)太离谱了起初有挂(透视)揭秘教程(2026已更新)(哔哩哔哩)您...
黑科技科技(微扑克职业代打)太... 黑科技科技(微扑克职业代打)太坑了原先有挂(透视)透明挂教程(2024已更新)(哔哩哔哩)1、玩家可...
黑科技教程(wepoke辅助工... 1、黑科技教程(wepoke辅助工具的使用教程)太实锤了素来真的有挂(透视)必备教程(2025已更新...
黑科技总结(wepoke辅助工... 黑科技总结(wepoke辅助工具的使用教程)太离谱了素来真的有挂(透视)wepoke教程(2024已...