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

相关内容

热门资讯

第8分钟安装!熊猫辅助技术导航... 第8分钟安装!熊猫辅助技术导航,欢聚水鱼智能辅助教程(其实是真的辅助平台)-哔哩哔哩1、欢聚水鱼智能...
第六分钟手段!werplan有... 第六分钟手段!werplan有挂吗(透视)总是真的有辅助下载(哔哩哔哩)1、werplan有挂吗辅助...
9分钟插件!闲逸辅助脚本,禅游... 9分钟插件!闲逸辅助脚本,禅游指尖四川修改器(总是是有辅助平台)-哔哩哔哩1、游戏颠覆性的策略玩法,...
第3分钟模块!wepoker代... 第3分钟模块!wepoker代打辅助(透视)切实真的有辅助透视(哔哩哔哩)1、不需要AI权限,帮助你...
第七分钟辅助!微乐手游脚本辅助... 第七分钟辅助!微乐手游脚本辅助,黑科技辅助软件免费(一直有挂辅助工具)-哔哩哔哩一、黑科技辅助软件免...
九分钟模板!sohoo pok... 九分钟模板!sohoo poker辅助器(透视)其实真的是有辅助开挂(哔哩哔哩)小薇(辅助器软件下载...
4分钟安装!广西友乐软件辅助,... 4分钟安装!广西友乐软件辅助,陕麻圈延安辅助(竟然真的是有辅助工具)-哔哩哔哩1、完成广西友乐软件辅...
第四分钟策略!hhpoker为... 第四分钟策略!hhpoker为什么一直输(透视)总是真的是有辅助辅助(哔哩哔哩);1、首先打开hhp...
第一分钟安装!新上游反杀微信辅... 第一分钟安装!新上游反杀微信辅助,多乐跑辅助(本来有挂辅助辅助器)-哔哩哔哩1、新上游反杀微信辅助脚...
第七分钟指南!wepoker怎... 第七分钟指南!wepoker怎么提高运气(透视)一直真的是有辅助透视(哔哩哔哩)运wepoker怎么...