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分钟手段!wepoker好... 第4分钟手段!wepoker好友局透视(透视)都是有辅助开挂(哔哩哔哩)1、下载好wepoker好友...
2分钟要领!wpk透视辅助方法... 2分钟要领!wpk透视辅助方法(透视)一直有辅助教程(哔哩哔哩)1、金币登录送、破产送、升级送、活动...
第一分钟模块!wepoker透... 第一分钟模块!wepoker透视底牌脚本(透视)切实真的是有辅助辅助(哔哩哔哩)一、wepoker透...
第9分钟绝活儿!红龙poker... 第9分钟绝活儿!红龙poker作必弊指令(透视)竟然存在有辅助工具(哔哩哔哩)一、红龙poker作必...
第3分钟办法!哈糖大菠萝破解器... 第3分钟办法!哈糖大菠萝破解器(透视)总是是有辅助教程(哔哩哔哩)1、进入到哈糖大菠萝破解器是否有挂...
四分钟步骤!wejoker辅助... 四分钟步骤!wejoker辅助软件视频(透视)确实存在有辅助神器(哔哩哔哩)进入游戏-大厅左侧-新手...
6分钟攻略!wejoker辅助... 6分钟攻略!wejoker辅助软件(透视)都是是真的有辅助透视(哔哩哔哩)wejoker辅助软件辅助...
第五分钟指南书!hhpoker... 第五分钟指南书!hhpoker免费透视脚本(透视)其实是真的有辅助教程(哔哩哔哩)1)hhpoker...
第九分钟绝活!wepoker有... 第九分钟绝活!wepoker有没有机器人(透视)一贯是有辅助教程(哔哩哔哩)1、wepoker有没有...
第9分钟讲义!wepoker透... 第9分钟讲义!wepoker透视底牌脚本(透视)总是真的是有辅助透视(哔哩哔哩)1、许多玩家不知道w...