如何通过单击模态窗口外的任何位置来关闭模态

时间:2021-07-25 19:40:43

I'm currently working on this website.

我目前正在这个网站上工作。

and I would like to add extra function where users can close the modal (which could be opened at the top-right corner of the grey sub-header) by clicking anywhere except for the modal box.

我想添加额外的功能,用户可以通过点击除模态框之外的任何地方关闭模态(可以在灰色子标题的右上角打开)。

Right now I can only close the modal when I click close at the top-right corner of the modal box or the black background.

现在,当我点击模态框右上角或黑色背景时,我只能关闭模态。

How could I make this possible?

我怎么能这样做?

1 个解决方案

#1


1  

You can set bigger value of z-index to black overlay (#modal-overlay) to make it appear above other elements.

您可以将较大的z-index值设置为黑色叠加(#modal-overlay),使其显示在其他元素上方。

For test reasons you can test this code in console :

出于测试原因,您可以在控制台中测试此代码:

$('#modal-overlay').css('z-index', 10000);
$('#modal-name, #modal-movies').css('z-index', 10001);

Of course you should set it in CSS and choose more reasonable z-index values ;)

当然你应该在CSS中设置它并选择更合理的z-index值;)

#1


1  

You can set bigger value of z-index to black overlay (#modal-overlay) to make it appear above other elements.

您可以将较大的z-index值设置为黑色叠加(#modal-overlay),使其显示在其他元素上方。

For test reasons you can test this code in console :

出于测试原因,您可以在控制台中测试此代码:

$('#modal-overlay').css('z-index', 10000);
$('#modal-name, #modal-movies').css('z-index', 10001);

Of course you should set it in CSS and choose more reasonable z-index values ;)

当然你应该在CSS中设置它并选择更合理的z-index值;)