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

相关内容

热门资讯

据玩家消息!边锋微信小程序(辅... 据玩家消息!边锋微信小程序(辅助)原来是有辅助脚本(有挂秘笈)-哔哩哔哩据玩家消息!边锋微信小程序(...
截至发稿!家乡大二辅助工具(辅... 截至发稿!家乡大二辅助工具(辅助)果然真的有辅助神器(有挂教学)-哔哩哔哩1、完成家乡大二辅助工具辅...
今年以来!多乐找刺激技巧辅助(... 今年以来!多乐找刺激技巧辅助(辅助)本来真的有辅助平台(有挂方式)-哔哩哔哩亲,关键说明,多乐找刺激...
一直以来!凑一桌游戏软件下载(... 一直以来!凑一桌游戏软件下载(辅助)果然是真的辅助器(新版有挂)-哔哩哔哩1)凑一桌游戏软件下载有没...
随着!微乐家乡官方app下载(... 随着!微乐家乡官方app下载(辅助)总是是有辅助平台(有挂头条)-哔哩哔哩;该软件可以轻松地帮助玩家...
此事迅速冲上热搜!八大碗辅助器... 此事迅速冲上热搜!八大碗辅助器app辅助(辅助)果然是有辅助软件(有挂攻略)-哔哩哔哩运八大碗辅助器...
现就发布提示!微信黑科技辅助神... 现就发布提示!微信黑科技辅助神器(辅助)确实是真的辅助挂(有挂分享)-哔哩哔哩1、全新机制【微信黑科...
据报道!腾威填大坑辅助(辅助)... 据报道!腾威填大坑辅助(辅助)确实是真的辅助app(有挂总结)-哔哩哔哩1、实时腾威填大坑辅助透视辅...
迎来新发展!518互游破解(辅... 迎来新发展!518互游破解(辅助)其实存在有辅助插件(有挂总结)-哔哩哔哩1、完成518互游破解有辅...
有玩家发现!pokemmo免费... 有玩家发现!pokemmo免费脚本,新518互游插件下载,教程书辅助神器(有挂细节)-哔哩哔哩1)新...