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

相关内容

热门资讯

第九分钟插件!微乐小程序免费黑... 第九分钟插件!微乐小程序免费黑科技,微乐辅助软件购买平台(开挂)切实是真的挂(2026)1)微乐小程...
目前来看!微乐小程序免费黑科技... 目前来看!微乐小程序免费黑科技,微乐游戏小程序辅助器免费苹果版(透视)其实真的有挂(哔哩哔哩)1、点...
十分钟脚本!微乐小程序免费黑科... 十分钟脚本!微乐小程序免费黑科技,2025微乐小程序黑科技(开挂)都是有挂(2026)1、任何微乐小...
出乎意料的是!微乐小程序黑科技... 出乎意料的是!微乐小程序黑科技,微信小程序微乐陕西挖坑(透视)本来真的是有挂(哔哩哔哩)1、进入到微...
6分钟教程!微乐小程序免费黑科... 6分钟教程!微乐小程序免费黑科技,微信小程序游戏辅助器(开挂)其实有挂(2026)1、微乐小程序免费...
据相关数据显示!微乐小程序免费... 据相关数据显示!微乐小程序免费黑科技,微乐自建房辅助工具(透视)果然有挂(哔哩哔哩)一、微乐小程序免...
9分钟辅助!微乐小程序黑科技,... 9分钟辅助!微乐小程序黑科技,微信微乐辅助透视(开挂)其实存在有挂(2026)进入游戏-大厅左侧-新...
插件辅助挂!微乐小程序免费黑科... 插件辅助挂!微乐小程序免费黑科技,微信小程序微乐辅助器苹果(透视)都是有挂(哔哩哔哩)1)微信小程序...
第1分钟透视!微乐小程序黑科技... 第1分钟透视!微乐小程序黑科技,微乐广西自建房免费黑科技下载(开挂)原来真的有挂(2026)微乐小程...
第7分钟脚本!微乐小程序免费黑... 第7分钟脚本!微乐小程序免费黑科技,微乐自建房辅助工具(开挂)确实有挂(2026)1、首先打开微乐小...