简体   繁体   English

如何启用在对话框外部单击

[英]How to enable click outside dialog box

I have a running Angular 9 application and I have created custom dialog box.我有一个正在运行的 Angular 9 应用程序,并且我创建了自定义对话框。 As of now, once the dialog box opens, I can only click on the buttons which is inside the dialog box.到目前为止,一旦对话框打开,我只能单击对话框内的按钮。

dialog.component.html dialog.component.html

<div class="modal">
<div class="modal-body">
    <ng-template #content></ng-template>
</div>

dialog.component.css dialog.component.css

.modal {
    display: block;
    position: absolute;
    z-index: 1;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
  }

  .modal-body{
    position: relative;
    padding: 10px;
    border: 1px solid;
    width: 50%;
    min-width: 50%;
    top: 100px;
    left: 0px;
  }

I want to achieve a behavior in which I want to enable click outside the dialog box:我想实现一种行为,我想在对话框外启用点击:

  1. Button which is inside the dialog box ( Close button in the below image) and the button which is outside the dialog box ( Click Me button in the below image) to be clickable.对话框内的按钮(下图中的关闭按钮)和对话框外的按钮(下图中的单击我按钮)可单击。

  2. Links ( Click Here to Open New Modal ) to be clickable which is outside dialog box链接(单击此处打开新模式)可单击,位于对话框之外

I want a generic solution which should work for every click( button, links, texts etc..) outside and inside dialog box.我想要一个通用的解决方案,它应该适用于对话框内外的每次点击(按钮、链接、文本等)。 Please help me on this.请帮助我。

在此处输入图像描述

Stackblitz demo: https://stackblitz.com/edit/dialog-box-overlay Stackblitz 演示: https://stackblitz.com/edit/dialog-box-overlay

https://stackblitz.com/edit/dialog-box-overlay-fphmxb https://stackblitz.com/edit/dialog-box-overlay-fphmxb

By using css you can add z-index to the button to make the button come above the backdrop and make it clickable:通过使用 css 您可以将z-index添加到按钮以使按钮位于背景上方并使其可点击:

.btn {
  position:absolute;
  bottom:0;
  z-index: 2;
}

The reason why click outside don't work now is because div class="modal" works as overlay for application.现在点击外部不起作用的原因是因为div class="modal"用作应用程序的覆盖。 If you don't want to change current html, you can use css solution for this:如果您不想更改当前的 html,您可以为此使用 css 解决方案:

Add pointer-events: none;添加pointer-events: none; style for modal backdrop (it's your div class="modal" )模态背景的样式(这是你的div class="modal"

Add pointer-events: auto;添加pointer-events: auto; for modal itselt (it's your div class="modal-body" )对于 modal itselt (这是你的div class="modal-body"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM