在模态对话框中淡入叠加

时间:2021-09-06 12:12:02

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;
 }

8 个解决方案

#1


2  

You can use the jQuery fadeTo() function. More information can be found on the link below. http://docs.jquery.com/Effects/fadeTo#speedopacitycallback

您可以使用jQuery fadeTo()函数。更多信息可以在下面的链接中找到。 http://docs.jquery.com/Effects/fadeTo#speedopacitycallback

#2


37  

You could also add this to fadeIn the modal:

您还可以将此添加到fadeIn模态中:

$(loginForm).dialog({
        resizable: false,
        open: function(){
            $('.ui-widget-overlay').hide().fadeIn();
        },
        show: "fade",
        hide: "fade" 
});

Hope this helps :)

希望这可以帮助 :)

#3


7  

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'容器,但是通过克隆它只是做淡化你可以回避它们的删除并仍然使用所有默认值样式。

#4


5  

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();
    });
}

#5


1  

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()和$(document).width()而不是body,因为后者的测量小于可见区。

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();
    });
}

#6


1  

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

#7


0  

$('.ui-widget-overlay').hide().fadeIn();

This effect has issue on IE as the opacity won't work after fade in

此效果在IE上有问题,因为淡入后不透明度不起作用

#8


0  

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();
  }
});

#1


2  

You can use the jQuery fadeTo() function. More information can be found on the link below. http://docs.jquery.com/Effects/fadeTo#speedopacitycallback

您可以使用jQuery fadeTo()函数。更多信息可以在下面的链接中找到。 http://docs.jquery.com/Effects/fadeTo#speedopacitycallback

#2


37  

You could also add this to fadeIn the modal:

您还可以将此添加到fadeIn模态中:

$(loginForm).dialog({
        resizable: false,
        open: function(){
            $('.ui-widget-overlay').hide().fadeIn();
        },
        show: "fade",
        hide: "fade" 
});

Hope this helps :)

希望这可以帮助 :)

#3


7  

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'容器,但是通过克隆它只是做淡化你可以回避它们的删除并仍然使用所有默认值样式。

#4


5  

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();
    });
}

#5


1  

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()和$(document).width()而不是body,因为后者的测量小于可见区。

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();
    });
}

#6


1  

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

#7


0  

$('.ui-widget-overlay').hide().fadeIn();

This effect has issue on IE as the opacity won't work after fade in

此效果在IE上有问题,因为淡入后不透明度不起作用

#8


0  

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();
  }
});