jQuery 在模态对话框中淡入叠加
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/751796/
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
Fade in overlay in modal dialog
提问by Deniz Dogan
I have a JQuery UI dialog which is modal and has a black background with 50% opacity. Is it possible to make the background opacity fade from 0% to 50%? If so, how? Because currently it feels kind of like getting a punch straight to the face when a dialog is shown.
我有一个 JQuery UI 对话框,它是模态的,背景为黑色,不透明度为 50%。是否可以使背景不透明度从 0% 淡化到 50%?如果是这样,如何?因为目前感觉有点像在显示对话框时直接对着脸一拳。
FWIW, this is the CSS I'm using at the moment:
FWIW,这是我目前使用的 CSS:
.ui-widget-overlay {
background: black;
opacity: 0.5;
filter: alpha(opacity = 50);
position: absolute;
top: 0;
left: 0;
}
采纳答案by sshow
You can use the jQuery fadeTo()
function. More information can be found on the link below.
http://docs.jquery.com/Effects/fadeTo#speedopacitycallback
您可以使用 jQueryfadeTo()
函数。可以在下面的链接中找到更多信息。
http://docs.jquery.com/Effects/fadeTo#speedopacitycallback
回答by Sam Barnes
You could also add this to fadeIn the modal:
您还可以将此添加到模式中的淡入淡出:
$(loginForm).dialog({
resizable: false,
open: function(){
$('.ui-widget-overlay').hide().fadeIn();
},
show: "fade",
hide: "fade"
});
Hope this helps :)
希望这可以帮助 :)
回答by Luke
This is an expansion on Liam Potter's answer. His works great for the fade in, but doesn't handle the fade out. I found this the easiest way to make the background also fade back out:
这是 Liam Potter 答案的扩展。他的作品非常适合淡入,但不处理淡出。我发现这是使背景也淡出的最简单方法:
beforeClose: function(){
$('.ui-widget-overlay:first')
.clone()
.appendTo('body')
.show()
.fadeOut(400, function(){
$(this).remove();
})
;
}
You can't do this in the "close" method because jQuery has already removed the '.ui-widget-overlay' container, however by cloning it to just do the fade you can sidestep their removal and still make use of all the default styles.
您不能在“关闭”方法中执行此操作,因为 jQuery 已经删除了 '.ui-widget-overlay' 容器,但是通过克隆它来执行淡入淡出,您可以避开它们的删除并仍然使用所有默认值样式。
回答by Liam Potter
I know this is an old question, but I came across it just now in a search, and feel I could help other people.
我知道这是一个老问题,但我刚刚在搜索中遇到了它,觉得我可以帮助其他人。
This could be improved I'm sure but this will fade in and out your overlay when using a jQuery UI dialog.
这可以改进我敢肯定,但是当使用 jQuery UI 对话框时,这会淡入和淡出您的叠加层。
open: function(){
$('.ui-widget-overlay').hide().fadeIn();
},
beforeClose: function(){
$('.ui-widget-overlay').remove();
$("<div />", {
'class':'ui-widget-overlay'
}).css(
{
height: $("body").outerHeight(),
width: $("body").outerWidth(),
zIndex: 1001
}
).appendTo("body").fadeOut(function(){
$(this).remove();
});
}
回答by pody
Just a minor improvement on Liam Potter's answer. If you want the overlay to be full-screen then you might change his code to use the $(document).height()
and $(document).width()
instead of the body, because the latter be measured smaller than the visible area.
只是对 Liam Potter 的回答略有改进。如果您希望覆盖全屏,那么您可能会更改他的代码以使用$(document).height()
and$(document).width()
而不是主体,因为后者的测量值小于可见区域。
open: function(){
$('.ui-widget-overlay').hide().fadeIn();
},
beforeClose: function(){
$('.ui-widget-overlay').remove();
$("<div />", {
'class':'ui-widget-overlay'
}).css({
height: $(document).height(),
width: $(document).width(),
zIndex: 1001
}).appendTo("body").fadeOut(function(){
$(this).remove();
});
}
回答by bmurashin
You could create your own widget extending $.ui.dialog to add overlay show and hide animations with accurate configuration using option.
您可以创建自己的小部件扩展 $.ui.dialog 以使用选项添加覆盖显示和隐藏动画的准确配置。
Another lacking functionality to close dialog by click on overlay is also easily added:
另一个缺乏通过单击覆盖关闭对话框的功能也很容易添加:
in some file, say jquery-ui.fsrc.dialog.js:
在某个文件中,说 jquery-ui.fsrc.dialog.js:
(function( $, undefined ) {
$.widget('fsrc.fsrc_dialog', $.ui.dialog, {
open: function() {
// some helpful vars
var self = this,
options = self.options;
// call parent method - it will create overlay and save it in self.overlay variable
var ret = $.ui.dialog.prototype.open.apply(this, arguments);
if (options.showOverlay) {
// immediately hide and animate overlay
// kind a hack, but jquery ui developers left no better way
self.overlay.$el.hide().show(options.showOverlay)
}
if (options.closeOnOverlay) {
// close dialog on click on overlay
self.overlay.$el.click(function() {
self.close();
})
}
return ret;
},
close: function(event) {
var self = this,
options = self.options;
if (options.hideOverlay) {
// save and unset self.overlay, so it will not be destroyed by parent function during animation
var overlay = self.overlay
self.overlay = null;
overlay.$el.hide(options.hideOverlay, function() {
// destroy overlay after animation as parent would do
overlay.destroy();
})
}
// call parent method
var ret = $.ui.dialog.prototype.close.apply(this, arguments);
return ret;
}
});
}(jQuery));
In page:
在页面:
<script src='/js/jquery-ui.fsrc.dialog.js' type='text/javascript'></script>
<script type="text/javascript">
<!--
jQuery(document).ready(function(){
jQuery('#show').click(function(){
jQuery('#order_form_container').fsrc_dialog({
modal: true,
closeOnOverlay: true,
show: {effect: "fade", duration: 500},
showOverlay: {effect: "fade", duration: 500},
hide: {effect: "fade", duration: 300},
hideOverlay: {effect: "fade", duration: 300},
});
})
})
-->
</script>`
I named namespace, widget and options to my favor.
我根据自己的喜好命名了命名空间、小部件和选项。
Tested jquery1.7.2, jquery-ui1.8.19, IE9, ff11, chrome18.0.1025.168m, opera11.61
测试jquery1.7.2、jquery-ui1.8.19、IE9、ff11、chrome18.0.1025.168m、opera11.61
回答by Moe Singh
I had to modify the answer from Sam Barnes to make it work (I also threw the dialog click function in a $(document).ready function):
我不得不修改 Sam Barnes 的答案以使其工作(我还在 $(document).ready 函数中抛出了对话框单击函数):
<script type='text/javascript'>
$(".dialog").click(function(){
$('.ui-widget-overlay').hide().fadeIn();
$("div.dialog").dialog({
resizable: false,
close: function(){
$('.ui-widget-overlay').hide();
},
show: "fade",
hide: "fade"
});
$(".ui-dialog").fadeIn();
return false;
});
$(".ui-widget-overlay").click(function(){
$(this).hide();
$(".ui-dialog").hide();
});
</script>
<style>
.ui-widget-overlay {
background: black;
opacity: 0.5;
filter: alpha(opacity = 50);
position: absolute;
top: 0;
left: 0;
}
</style>
<div class='ui-widget-overlay'></div>
<div class='dialog' title='Heading!'>Some Message!</div>
You can add the thing that hides on escape button press by adding:
您可以通过添加以下内容来添加在按下退出按钮时隐藏的内容:
$(document).keyup(function(e) {
if (e.keyCode == 27) {
$(".ui-dialog").hide();
$('.ui-widget-overlay').hide();
}
});
回答by wildwindfeng
$('.ui-widget-overlay').hide().fadeIn();
This effect has issue on IE as the opacity won't work after fade in
这种效果在 IE 上有问题,因为淡入后不透明度将不起作用