如何延迟在 CDK 覆盖中打开



当用户将鼠标悬停在列表项上时,我正在使用 CDK 覆盖显示"弹出框"。我目前在鼠标输入事件触发时打开弹出窗口。

我的代码:

//component.html
<mat-list-item *ngFor="let item of itemList" (mouseenter)="showItemDetail(item)">
    {{item.display}}
</mat-list-item>


//component.ts
showItemDetail(item: IItemDto, event: MouseEvent) {
        this.hideItemDetail(); // Closes any open overlays
        this.itemDetailOverlayRef = this.itemDetailOverlayService.open(item);
}

//itemDetailOverlayService.ts
open(item: IItemDto) {
        // Returns an OverlayRef (which is a PortalHost)
        const overlayRef = this.createOverlay(item);
        const dialogRef = new ItemDetailOverlayRef(overlayRef);
        // Create ComponentPortal that can be attached to a PortalHost
        const itemDetailPortal = new ComponentPortal(ItemDetailOverlayComponent);
        const componentInstance = this.attachDialogContainer(overlayRef, item, dialogRef);
        // Attach ComponentPortal to PortalHost
        return dialogRef;
}

private attachDialogContainer(overlayRef: OverlayRef, item: IItemDto, dialogRef: ItemDetailOverlayRef) {
        const injector = this.createInjector(item, dialogRef);
        const containerPortal = new ComponentPortal(ItemDetailOverlayComponent, null, injector);
        const containerRef: ComponentRef<ItemDetailOverlayComponent> = overlayRef.attach(containerPortal);
        return containerRef.instance;
}

请注意,我的覆盖依赖于列表项数据中的数据。

如何延迟showItemDetail()仅在 2 秒后打开叠加层?请记住,一次只能打开一个弹出框。

setTimeout()显然不起作用,因为如果用户在项目列表中拖动鼠标,则会打开多个弹出框。

通过使用 css 动画/关键帧创建延迟效果时立即打开叠加层来解决:

.container {
    animation: fadeIn 1.5s linear;
}
@keyframes fadeIn {
    0% {
        opacity: 0;
    }
    75% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}

最新更新