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

相关内容

热门资讯

第8分钟要领!福建老友破解(外... 第8分钟要领!福建老友破解(外挂)确实真的有辅助教程(哔哩哔哩)1、这是跨平台的福建老友破解轻量版有...
第四分钟绝活儿!广东雀神智能插... 第四分钟绝活儿!广东雀神智能插件免费(外挂)好像真的是有辅助app(哔哩哔哩)亲,关键说明,广东雀神...
7分钟法门!四川游戏家园手机版... 7分钟法门!四川游戏家园手机版辅助器(外挂)其实真的有辅助插件(哔哩哔哩)1、下载好四川游戏家园手机...
9分钟讲义!武汉约战辅助(外挂... 9分钟讲义!武汉约战辅助(外挂)好像是有辅助软件(哔哩哔哩)1、进入游戏-大厅左侧-新手福利-激活码...
第七分钟绝活儿!可可茶馆辅助工... 第七分钟绝活儿!可可茶馆辅助工具(外挂)切实是真的有辅助技巧(哔哩哔哩)1、全新机制【可可茶馆辅助工...
5分钟模块!新九游辅助(外挂)... 5分钟模块!新九游辅助(外挂)确实有辅助方法(哔哩哔哩)新九游辅助透视方法中分为三种模型:新九游辅助...
7分钟课程!hhpoker辅助... 7分钟课程!hhpoker辅助器下载(外挂)一贯是有辅助软件(哔哩哔哩)1、每一步都需要思考,不同水...
1分钟方针!八闽状元郎破解版辅... 1分钟方针!八闽状元郎破解版辅助(外挂)确实是真的有辅助技巧(哔哩哔哩)1、八闽状元郎破解版辅助免费...
第八分钟手段!微乐小程序辅助开... 第八分钟手段!微乐小程序辅助开挂(外挂)其实真的是有辅助教程(哔哩哔哩)1、微乐小程序辅助开挂破解器...
第4分钟项目!黑科技软件大全(... 第4分钟项目!黑科技软件大全(外挂)竟然是真的有辅助软件(哔哩哔哩);1、黑科技软件大全有没有辅助教...