javascript 在子 div 外部单击时关闭灯箱

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29440518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:32:20  来源:igfitidea点击:

Close lightbox on click outside of the child div

javascriptjqueryhtml

提问by Danijel

I'm creating a lightbox without using a jquery plugin, and now I'm trying to close it by clicking on the closebutton or by clicking anywhere else outside of the white area (.white_content)
Jsfiddle Example

我正在创建一个灯箱而不使用 jquery 插件,现在我试图通过单击close按钮或单击白色区域之外的任何其他位置来关闭它( .white_content)
Jsfiddle 示例

<button onclick="document.getElementById('lightbox').style.display='inline';">
    Show lightbox
</button>

<!-- LIGHTBOX CODE BEGIN -->
<div id="lightbox" class="lightbox" style="display:none">
    <div class="white_content">
        <a href="javascript:void(0)" onclick="document.getElementById('lightbox').style.display='none';" class="textright">Close</a>
            <p>Click anywhere to close the lightbox.</p>
            <p>Use Javascript to insert anything here.</p>
    </div>
</div>
<!-- LIGHTBOX CODE END -->

Although it's not just like I want it. I want it to close only if I click on the dark area of the lightbox and not on the white container (.white_content), I've heard that event.propagation can be a bad thing to use, so here's how I'm closing the lightbox

虽然这不仅仅是我想要的。我希望它仅在我点击灯箱的黑暗区域而不是白色容器 ( .white_content) 时关闭,我听说 event.propagation 使用起来可能不好,所以这是我关闭灯箱的方法

$(document).on('click', function(event) {
  if (!$(event.target).closest('button').length) {
    $(".lightbox").hide();
  }
});

回答by Hardik Panchal

you can change you condition bit like below

你可以像下面一样改变你的条件

$(document).on('click', function(event) {
    if ($(event.target).has('.white_content').length) {
        $(".lightbox").hide();
    }
});

回答by Danijel

Most lightbox scripts are using two div-s, content and overlay. Overlay is there for background and to prevent users to click on page content, and also click on overlay can be used to close lightbox.

大多数灯箱脚本使用两个 div-s,内容和覆盖。Overlay 用于背景和防止用户点击页面内容,也可以点击overlay 关闭灯箱。

HTML:

HTML:

<div id="lightbox"> LIGHTBOX CONTENT </div>
<div id="overlay"></div>

JS:

JS:

$( '#overlay, #close').on('click', function(event) {
    $("#lightbox, #overlay").hide();
});

$( '#show').on('click', function(event) {
    $("#lightbox, #overlay").show();
});

EXAMPLE

例子

回答by Will

You want to close the lightbox on any click that isn't targeting the lightbox or one of its children. Your existing code is pretty close:

您希望在不针对灯箱或其子项之一的任何点击时关闭灯箱。您现有的代码非常接近:

$(document).on('click', function(event) { 
    if (!$(event.target).closest('button').length &&
        !$(event.target).closest('.white_content').length) {
        $(".lightbox").hide();
    }
});

$(document).on('click', function(event) { 
    if (!$(event.target).closest('button').length &&
        !$(event.target).closest('.white_content').length) {
     $(".lightbox").hide();
    }
});
.textright {
    float: right;
}
.lightbox {
    position:fixed;
    top:0;
    left:0;
    width:100%;
    height:100%;
    background:rgba(0, 0, 0, .8);
}

.white_content {
    position: absolute;
    top: 25%;
    left: 25%;
    width: 50%;
    height: 50%;
    padding: 16px;
    border: 5px solid gray;
    background-color: white;
    z-index:1002;
    overflow: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="document.getElementById('lightbox').style.display='inline';">
    Show lightbox
</button>

<!-- LIGHTBOX CODE BEGIN -->
<div id="lightbox" class="lightbox" style="display:none">
    <div class="white_content">
        <a href="javascript:void(0)" onclick="document.getElementById('lightbox').style.display='none';" class="textright">Close</a>
            <p>Click anywhere to close the lightbox.</p>
            <p>Use Javascript to insert anything here.</p>
    </div>
</div>
<!-- LIGHTBOX CODE END -->

I'd also recommend using a class to denote whether the lightbox is visible or not, rather than changing the display property directly; that way it's more clear when you check it. Compare $el.is('.active')with $(el).css('display') == 'inline'

我还建议使用一个类来表示灯箱是否可见,而不是直接更改显示属性;这样你检查的时候就更清楚了。比较$el.is('.active')$(el).css('display') == 'inline'