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

相关内容

热门资讯

玩家必看科普!wepoker有... 很多人使用云端 AI 时,都担心wepoker到底有没有透视、内部资料上传带来隐私风险。这款本地运行...
2分钟细说!wpk透视插件,德... 您好,2分钟细说!wpk透视插件,德扑之星确实有挂教程(一向真的是有辅助挂)这款游戏可以开挂的,确实...
重大消息!德普之星app安卓版... AI 黑科技赛道又上新,这款智能处理工具堪称效率开挂,专门面向短视频创作者打造。它打通脚本撰写、AI...
记者发布!wepoker私人局... wepoker私人局怎么玩AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏攻...
一分钟了解!!wpk辅助软件,... 本地部署 AI 黑科技工具迎来新版本,不用上传文件到云端就能完成wpk辅助软件解析、 AI 处理、w...
技术分享!如何下载wpk透视版... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
解密教程!wepoker免费辅... wepoker免费辅助器AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏攻略...
记者揭秘!!wpk透视工作室,... 一款 AI 游戏攻略黑科技工具上线,查攻略效率直接开挂。它整合版本更新、装备搭配、BOSS 打法、牌...
解密教程!wepokerplu... 您好,解密教程!wepokerplus开挂,德普之星可以开挂吗平台(切实有透视)这款游戏可以开挂的,...
玩家必看!德普之星辅助工具如何... 本地部署 AI 黑科技工具迎来新版本,不用上传文件到云端就能完成德普之星辅助工具如何打开解析、 AI...