当用户将鼠标悬停在列表项上时,我正在使用CDK叠加层显示“弹出窗口”。目前,当mouseenter事件触发时,我会打开弹出窗口。
我的代码:
//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()
显然不起作用,因为如果用户在项目列表上拖动鼠标,则会打开多个弹出窗口。
答案 0 :(得分:0)
通过使用css动画/关键帧创建延迟效果时无延迟地打开叠加层来解决:
.container {
animation: fadeIn 1.5s linear;
}
@keyframes fadeIn {
0% {
opacity: 0;
}
75% {
opacity: 0;
}
100% {
opacity: 1;
}
}