1.页面滚动时关闭下拉框
//给需要监测滚动的下拉框添加
openEvent(param) {
this.selectOpenState[param] = true;
this.scrollSubscription != undefined && this.scrollSubscription.unsubscribe();
this.listenScroll();
}
<nz-select
[nzOpen]="selectOpenState['state1']"
(click)="openEvent('state1')">
<nz-option *ngFor="let i of data"
[nzValue]="i" [nzLabel]="i.name">
</nz-option>
</nz-select>
listenScroll(): void {
this.scrollSubscription = fromEvent(document.querySelector('.滚动作用域的class'), 'scroll')
.pipe(debounceTime(10))
.subscribe(() => {
for (const key in this.selectOpenState) {
this.selectOpenState[key] = false;
}
});
}
2.给点击事件添加阻止冒泡
click():void{
document.querySelector('.作用域')?.addEventListener('click', (e) => {
e.stopPropagation();
})
}