Fade in overlay in modal dialog

后端 未结 8 2360
情书的邮戳
情书的邮戳 2021-02-07 05:11

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 c

相关标签:
8条回答
  • 2021-02-07 05:34

    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:

    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.

    0 讨论(0)
  • 2021-02-07 05:37

    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.

    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();
        });
    }
    
    0 讨论(0)
提交回复
热议问题