4 回答
TA贡献1817条经验 获得超14个赞
您的叠加 div 应该在顶部关闭。试试这个
<div id="overlay" (click)="closeThis($event)" *ngIf="open" class="overlay"></div>
<div class="bg-modal" id="bg-modal">
<div class="modal-content" id="openModal">
<div class="col-12">
<div class="close" id="close" (click)="closeThis($event)">+</div>
<p class="title">Modal</p>
<hr>
</div>
<div class="col-12">
<div class="contents">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequ.</p>
</div>
</div>
<div class="confirm" id="confirm" (click)="confirmModal()">OK</div>
</div>
</div>
.overlay {
position: fixed;
width: 100vw;
height: 100vh;
left: 0;
top: 0;
background: rgba(0,0,0,0.5);
}
.bg-modal{
position: fixed;
z-index: 10
}
TA贡献2003条经验 获得超2个赞
在你的模态周围有一个叠加,比如:-
<button (click)="openModalBtn()" id="bt">
{{buttonName}}
</button>
<div id="overlay" (click)="closeThis($event)" *ngIf="open" class="overlay">
<div class="bg-modal" id="bg-modal">
<div class="modal-content" id="openModal">
<div class="col-12">
<div class="close" id="close" (click)="closeThis($event)">+</div>
<p class="title">Modal</p>
<hr>
</div>
<div class="col-12">
<div class="contents">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequ.</p>
</div>
</div>
<div class="confirm" id="confirm" (click)="confirmModal()">OK</div>
</div>
</div>
</div>
和 css :-
.overlay {
position: fixed;
width: 100vw;
height: 100vh;
left: 0;
top: 0;
background: transparent;
}
TS :-
public closeThis(event) {
if(event.target.id==='close' || event.target.id ==='overlay') {
this.open= false;
}
}
TA贡献1824条经验 获得超6个赞
侦听窗口单击事件并获取对模式的引用。在窗口中,单击检查模式是否包含目标元素。如果它不包含,请关闭模态。Viewchild
@ViewChild("<modal-referece>",{static:false}) modalRef:ElementRef;
@HostListener('window:click', ['$event.target'])
onClick(elem) {
let element: HTMLElement = elem as HTMLElement;
if(!(this.modalRef.nativeElement as HTMLElement).contains(element)){
//close modal
}
}
TA贡献2041条经验 获得超4个赞
尝试另一种方式来侦听点击事件:
import { Component, OnInit, ElementRef, HostListener, AfterViewInit } from '@angular/core';
@Component({
selector: 'app-modal',
templateUrl: './modal.component.html',
styleUrls: ['./modal.component.css']
})
export class ModalComponent implements OnInit, AfterViewInit {
public open: boolean = false;
public buttonName: any = 'Open';
modalElement: any;
constructor(private elementRef: ElementRef) { }
@HostListener('document:click', ['$event'])
clickout(event) {
if (!this.elementRef.nativeElement.contains(event.target)) {
this.closeAll();
}
}
ngAfterViewInit() {
this.modalElement = this.elementRef.nativeElement.querySelector('#bg-modal');
}
ngOnInit() {
}
openModalBtn() {
this.open = !this.open;
}
public closeThis(): void {
this.open = false;
}
public confirmModal(): void {
this.open = false;
}
public closeAll(): void {
this.open = false;
}
}
添加回答
举报