Fade in overlay in modal dialog

后端 未结 8 2288
情书的邮戳
情书的邮戳 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:14

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

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

    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 :)

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

    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.

    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();
        });
    }
    
    0 讨论(0)
  • 2021-02-07 05:21

    You could create your own widget extending $.ui.dialog to add overlay show and hide animations with accurate configuration using option.

    Another lacking functionality to close dialog by click on overlay is also easily added:

    in some file, say 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

    0 讨论(0)
  • 2021-02-07 05:24
    $('.ui-widget-overlay').hide().fadeIn();
    

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

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

    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):

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