twitter-bootstrap 在对话框外单击时,twitter boostrap 模态触发器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14605598/
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
twitter boostrap modal trigger when clicks on outside the dialog
提问by Manivannan Jeganathan
Is there an option in the bootstrap modal to trigger the event if the user clicks outside the dialog?
如果用户在对话框外单击,引导模式中是否有触发事件的选项?
For example: I would like to add animated shakecss when user click away from modal.
例如:当用户单击远离模态时,我想添加动画抖动css。
回答by Andrea Turri
Yes, using the event shownyou can fire a new function that when someone click on the body (so outside the modal) you do something.
是的,使用事件显示,你可以触发一个新的功能,当身体上有人点击(所以外的模态)你做的东西。
Remember to add also event.stopPropagation()to don't close the modal when you click outside.
请记住还要添加event.stopPropagation()不要在单击外部时关闭模态。
Here an example:
这里有一个例子:
$('#myModal').on('shown', function () {
$('body').on('click', function(e) {
// your function...
e.stopPropagation();
});
})
$('#myModal').on('hidden', function () {
$('body').off('click');
});
回答by HellSea
$('#myModal').on('hidden.bs.modal', function (e) {
// do something...
})
(Source: http://getbootstrap.com/javascript/)
回答by John Mc
This isn't specifically for Bootstrap, but it should be possible to do what you asked about.
这不是专门针对 Bootstrap 的,但是应该可以按照您的要求进行操作。
The following Code snippet detects a click anywhere on the page. It then examines what was clicked on and decides what to do based on that.
以下代码片段检测页面上任意位置的点击。然后它检查点击了什么,并根据它决定要做什么。
$(document).click(function (e) {
//Dont hide the button if it's a link or input that was clicked
if ((e.target.nodeName == 'A') || (e.target.nodeName == 'INPUT')) return;
HideSubmenus();
});
In your case, you could probably check if it's not a Div (and thereby not the Modal).
在您的情况下,您可能会检查它是否不是 Div(因此不是 Modal)。
回答by Abhijeet Sharma
Animated Shakes is the default feature of Modal popover. You just need to set backdrop:'static'to do this. This will prevent the popover to close and enable shaking animation when someone will click outside of modal.
动画震动是模态弹出窗口的默认功能。你只需要设置background:'static'来做到这一点。这将防止弹出窗口关闭并在有人在模态之外单击时启用抖动动画。
$('#myModal').modal({
backdrop:'static', show: true
})
For more information please have a look at this
有关更多信息,请查看此

