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透视是真的假的,微乐自建房脚本下载,机巧教程(存在有挂)-哔哩哔哩在进入wpk透视是...
普及透视!德州透视hhpoke... 普及透视!德州透视hhpoker,微乐小程序免费黑科技在线下载,指南书教程(有挂存在)-哔哩哔哩1、...
分享透视!hhpoker辅助器... 分享透视!hhpoker辅助器视频,微乐四川麻将辅助器,法门教程(有挂分享)-哔哩哔哩hhpoker...
必备透视!pokemmo辅助脚... 必备透视!pokemmo辅助脚本,微乐江苏小程序游戏破解器下载,模板教程(有挂教学)-哔哩哔哩1、每...
解密透视!wepoker辅助软... 解密透视!wepoker辅助软件视频,微乐宁夏小程序插件,讲义教程(有挂辅助)-哔哩哔哩1、在wep...
揭露透视!hhpkoer辅助器... 您好,hhpkoer辅助器这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很...
解迷透视!wepoker透视脚... 解迷透视!wepoker透视脚本下载,微乐自建房辅助工具别人怎么看,积累教程(真的有挂)-哔哩哔哩1...
分享透视!wepoker私局代... 分享透视!wepoker私局代打,微信小程序怎么开挂,手段教程(有挂教学)-哔哩哔哩一、wepoke...
辅助透视!德普之星辅助器,微乐... 辅助透视!德普之星辅助器,微乐春天扑克开挂辅助插件,攻略教程(有挂助手)-哔哩哔哩1、许多玩家不知道...
教你透视!德普软件,广西微乐小... 教你透视!德普软件,广西微乐小程序控制器,课程教程(有挂猫腻)-哔哩哔哩1、超多福利:超高返利,海量...