简体   繁体   English

单击框外单击关闭模态

[英]Close modal by clicking outside the box

Can I use simple css to close modal by clicking outside the box? 我可以通过单击框外单击使用简单的css来关闭模态吗? I have seen examples of how to do it using jQuery/JavaScript. 我已经看到了如何使用jQuery / JavaScript做到这一点的示例。 I have it set up right now so that it closes when clicking the 'x' and no JavaScript is being used: 我现在设置它以便在单击“x”时关闭并且没有使用JavaScript:

<div><a href="#close" title="Close" class="close">X</a></div> And then in my css file: <div><a href="#close" title="Close" class="close">X</a></div>然后在我的css文件中:

.close {
 opacity: 10px;
 background-color: darkblue;
 color: #FFFFFF;
 line-height: 25px;
 position: absolute;
 right: -12px;
 text-align: center;
 top: -10px;
 width: 24px;
 text-decoration: none;
 font-weight: bold;
 -webkit-border-radius: 12px;
 -moz-border-radius: 12px;
 border-radius: 12px;
 -moz-box-shadow: 1px 1px 3px #000;
 -webkit-box-shadow: 1px 1px 3px #000;
  box-shadow: 1px 1px 3px #000;
}
.close:hover {
  background: #00d9ff;
}

This can't be accomplished with just plain CSS. 仅使用纯CSS就无法实现这一点。

Javascript is there to make your page dynamic and reactive, so you should be using it to listen for events and for manipulating what is shown to the user. Javascript可以让您的页面变得动态和被动,因此您应该使用它来监听事件并操纵显示给用户的内容。

Rather than using CSS you could use a button that calls a Javascript function to open the modal like so: 您可以使用一个调用Javascript函数的按钮来打开模态,而不是使用CSS,如下所示:

jQuery: jQuery的:

<button id="modal-button" onclick="openModal();">Open Modal</button>

HTML: HTML:

<script>

function openModal()
{
    $('#myModal').modal('show');
}

</script>

Using this method you will be able to click off the modal to close it. 使用此方法,您可以单击关闭模式以关闭它。

If you can alter the html and place a hidden checkbox and an extra overlay before the modal, then yes, I have a solution for you. 如果你可以改变html并在模态之前放置一个隐藏的checkbox和一个额外的叠加层,那么是的,我有一个解决方案。

HTML HTML

<input type="checkbox" id="modal-toggle" class="modal-toggle" />
<label for="modal-toggle" class="modal-overlay"></label>
<div class="modal">
    <label for="modal-toggle" class="modal-close-button">X</label>
</div>

CSS CSS

.modal-toggle,
.modal-overlay,
.modal {
    display: none;
}

.modal-toggle:checked + .modal-overlay,
.modal-toggle:checked + .modal-overlay + .modal {
    display: block;
}

.modal-overlay {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 1;
}

.modal {
    position: absolute;
    /* I've used absolute here to note that the modal can't be static */
    /* add other properties to position this div */
    z-index: 2;
}

From w3schools.com : 来自w3schools.com

Note: z-index only works on positioned elements (position:absolute, position:relative, or position:fixed). 注意:z-index仅适用于定位元素(位置:绝对,位置:相对或位置:固定)。

How does it work? 它是如何工作的? We have a hidden overlay and modal right after an input. 输入后我们有一个隐藏的叠加和模态。 When this input gets checked the overlay and modal will be shown. 检查此输入后,将显示叠加和模态。 The overlay and the close button are the labels of the checkbox so clicking on these will uncheck the input, thus hides the modal. 叠加层和关闭按钮是复选框的标签,因此单击这些将取消选中输入,从而隐藏模态。 You will need another label somewhere in your html which will bring up the modal of course. 你需要在你的HTML中的某个地方使用另一个标签,当然会提出模态。

You can read about the "+" css selector here . 你可以在这里阅读“+”css选择器

Full list of css selectors 完整的CSS选择器列表

You can use multiple modals on the same page, just make sure every modal has its own unique id and for attribute value. 您可以在同一页面上使用多种模态,只要确保每一个模式都有其独特id ,并for属性值。 The question didn't mention if the modal has to be animated on show/hide, that is possible too. 问题没有提到模式是否必须在show / hide上进行动画处理,这也是可能的。

you can close the div by clicking out side 你可以点击一边关闭div

add this code to your js file or inside your <script> </script> tag 将此代码添加到您的js文件或<script> </script>标记内

replace the ID_OF_DIV with id of the div you want to close ID_OF_DIV替换为要关闭的div的id

document.body.addEventListener("click", function() {
    var element = document.getElementById("ID_OF_DIV");
    if (element) {
        element.style.display = "none";
    }
});

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

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