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

相关内容

热门资讯

免费测试版!WPK辅助挂多功能... 免费测试版!WPK辅助挂多功能透视工具,真是是有挂(真的有挂)1、很好的工具软件,可以解锁游戏的WP...
玩家必看科普!AAPoKer辅... 玩家必看科普!AAPoKer辅助挂多功能透视工具,其实有挂(揭秘有挂)1、AAPoKer有没有辅助教...
一分钟揭秘!AAPoKer辅助... 一分钟揭秘!AAPoKer辅助挂多功能透视工具,都是有挂(揭秘有挂)1、AAPoKer模拟器是什么优...
今日科普!微扑克辅助挂多功能透... 今日科普!微扑克辅助挂多功能透视工具,果然真的有挂(有挂工具)1、微扑克模拟器是什么优化,微扑克俱乐...
终于懂了!微扑克辅助挂多功能透... 终于懂了!微扑克辅助挂多功能透视工具,真是真的有挂(确实有挂)一、微扑克可以开透视的定义与意义1、微...
玩家实测!智星德州扑克辅助挂多... 玩家实测!智星德州扑克辅助挂多功能透视工具,切实存在有挂(有挂教程)1、操作简单,无需智星德州扑克手...
我来教大家!德扑圈辅助挂多功能... 我来教大家!德扑圈辅助挂多功能透视工具,竟然是真的有挂(讲解有挂)1、该软件可以轻松地帮助玩家将德扑...
查到实测辅助!德普之星辅助挂多... 查到实测辅助!德普之星辅助挂多功能透视工具,切实是有挂(有挂技巧)1、玩家可以在德普之星线上大神俱乐...
重大推荐!红龙poker辅助挂... 重大推荐!红龙poker辅助挂多功能透视工具,果然是真的有挂(竟然有挂)1、每一步都需要思考,不同水...
发现玩家!智星德州扑克辅助挂多... 发现玩家!智星德州扑克辅助挂多功能透视工具,竟然真的有挂(真的有挂)1、很好的工具软件,可以解锁游戏...