为了账号安全,请及时绑定邮箱和手机立即绑定

在背景上关闭模态

在背景上关闭模态

临摹微笑 2022-09-02 21:32:22
我在Angular 4中使用html,css和typescript制作了一个模态。模式正在工作,打开和关闭,但我似乎无法点击背景来关闭模式,因为现在它只是在按钮X上关闭。代码是:<button (click)="openModalBtn()" id="bt">  {{buttonName}}</button><div class="bg-modal" id="bg-modal" *ngIf="open">      <div class="modal-content" id="openModal">        <div class="col-12">          <div class="close" id="close" (click)="closeThis()">+</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>&  public open: boolean = false;    public buttonName: any = 'Open';      ngOnInit() {    }    openModalBtn() {        this.open = !this.open;    }    public closeThis(): void {        this.open = false;    }     public confirmModal(): void {        this.open = false;    }    public closeAll(): void{         this.open = false;    }我尝试将其全部放在另一个div元素中,它可以通过单击任意位置来关闭,但这意味着单击模式内的任意位置也会关闭它。帮助?
查看完整描述

4 回答

?
大话西游666

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

}


查看完整回答
反对 回复 2022-09-02
?
湖上湖

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;

   }

}


查看完整回答
反对 回复 2022-09-02
?
慕妹3242003

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

    }

}


查看完整回答
反对 回复 2022-09-02
?
缥缈止盈

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;

    }

}


查看完整回答
反对 回复 2022-09-02
  • 4 回答
  • 0 关注
  • 73 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信