简体   繁体   English

在 Angular 中启用诸如单击、选择等操作...

[英]Enable actions like click, selecting etc… outside modal window in Angular

I have created custom dialog box in Angular.我在 Angular 中创建了自定义对话框。 The current behavior of dialog box is that user can only click on the buttons which is inside the dialog box.对话框的当前行为是用户只能单击对话框内的按钮。

dialog-box.component.html对话框.component.html

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

dialog-box.component.css对话框.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 ) and the button which is outside the dialog box ( Click Me button) 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

Understand the root cause of the problem -了解问题的根本原因——

You have added a div with class .modal which is absolutely positioned, and acquire the full screen space.您添加了一个绝对定位的带有 class .modal的 div,并获得了全屏空间。 On the top of it, your actual modal-body exists.最重要的是,您的实际模态体存在。 But since .modal div acquires the full screen space, it prevents all the elements below the modal to be clicked.但是由于.modal div 获得了全屏空间,它阻止了模态下方的所有元素被点击。 That is why when you try to click the click me button outside the modal, modal is actually getting clicked and not the button.这就是为什么当您尝试单击模态外部的click me按钮时,实际上是单击模态而不是按钮。

See, when you hover on the modal div in elements panel, you will be able to see the full blue screen.看,当您在元素面板中的模态 div 上 hover 时,您将能够看到整个蓝屏。

在此处输入图像描述

Solution -解决方案 -

This can be fixed with the help of CSS only.这只能在 CSS 的帮助下修复。 Replace your dialog-box.component.scss with this code and give it a try!用此代码替换您的dialog-box.component.scss并试一试!


.dialog-modal {
  padding: 1%;

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

  .modal-body {
    pointer-events: all;
    background-color: grey;
    position: relative;
    padding: 10px;
    border: 1px solid;
    width: 50%;
    height: 50%;
    min-width: 50%;
    top: 100px;
    left: 50px;
  }

}

Read more about thepointer events from here. 从这里阅读更多关于指针事件的信息。

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

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