【发布时间】:2021-09-21 00:20:33
【问题描述】:
我正在尝试创建一个弹出窗口,其中容器只是具有一些不透明度的黑色背景,然后该 div 容器包含弹出窗口的内容。
基本上它看起来像:
<button (click)="showPopup = !showPopup">Open popup</button>
<div class="overlay-bg" (click)="showPopup = !showPopup" [ngClass]="showPopup ? 'is-active' : ''">
<div class="content">Some content</div>
</div>
CSS 看起来像这样:
.overlay {
width: 100vw;
height: 100vh;
background: rgba(0, 0, 0, 0.5);
display: grid;
position: fixed;
pointer-events: none;
opacity: 0;
&.is-active {
opacity: 1;
pointer-events: all;
}
.content {
width: 400px;
height: 400px;
place-self: center;
background: red;
}
}
所以基本上,当状态未激活时,它不会显示,当通过单击按钮启用is-active 时,会显示叠加层+内容。现在,我还想要的是,当单击背景时,弹出窗口应该关闭 - 确实如此。但是,问题是,当我单击内容时,弹出窗口也会关闭——它不应该这样,因为它应该具有一定的交互性。
那么如何防止弹出窗口在与内容 div 交互时关闭?
【问题讨论】:
-
我不知道
(click)="showPopup = !showPopup"这个语法是什么,但是如果你使用纯javascript并添加一个事件处理程序,你会得到event参数。检查event.target以了解点击事件的来源。if event.target === <overlay-bg-reference>然后只执行showPopup = !showPopup -
你能在stackblitz中重现问题并在这里分享吗?