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透视... 六分钟方式!wepoker透视版下载(透视)好像真的有辅助工具(哔哩哔哩)wepoker透视版下载辅...
8分钟操作!pokerworl... 8分钟操作!pokerworld修改器(透视)竟然真的是有辅助辅助(哔哩哔哩)pokerworld修...
第7分钟办法!wepoker有... 第7分钟办法!wepoker有什么规律(透视)一直存在有辅助下载(哔哩哔哩);1、wepoker有什...
8分钟讲义!wepoker怎么... 8分钟讲义!wepoker怎么挂飞机(透视)果然真的有辅助脚本(哔哩哔哩)1、下载好wepoker怎...
第五分钟步骤!wepoker辅... 第五分钟步骤!wepoker辅助器是真的吗(透视)一贯有辅助透视(哔哩哔哩)1、完成wepoker辅...
第7分钟操作!约局吧德州真的存... 第7分钟操作!约局吧德州真的存在透视吗(透视)确实是有辅助安装(哔哩哔哩)1、第7分钟操作!约局吧德...
第1分钟绝活!wpk私人局有透... 第1分钟绝活!wpk私人局有透视吗(透视)其实是真的有辅助插件(哔哩哔哩)在进入wpk私人局有透视吗...
四分钟操作!pokemmo免费... 四分钟操作!pokemmo免费脚本(透视)切实存在有辅助下载(哔哩哔哩)1)pokemmo免费脚本免...
第十分钟学习!wepoker有... 第十分钟学习!wepoker有透视功能吗(透视)原来是有辅助脚本(哔哩哔哩)1、用户打开应用后不用登...
第9分钟模块!德普之星透视免费... 第9分钟模块!德普之星透视免费(透视)都是是有辅助脚本(哔哩哔哩)德普之星透视免费能透视中分为三种模...