`
xinklabi
  • 浏览: 1563479 次
  • 性别: Icon_minigender_1
  • 来自: 吉林
文章分类
社区版块
存档分类
最新评论

jQuery弹出层插件–BlockUI

 
阅读更多
  1. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>  
  2. <script src="images/jquery.blockUI.js" type="text/javascript"></script>  

 

导入库文件后,就可以调用BlockUI了,BlockUI的调用挺简单的,看下面这段代码

 

 

  1. $(function(){  
  2. $('#box_btn').click(function(){   //给box_btn绑定一个鼠标点击的事件  
  3.         $.blockUI({    //当点击事件发生时调用弹出层  
  4.              message: $('#box'),    //要弹出的元素box  
  5.              css: {    //弹出元素的CSS属性  
  6.                  top: '50%',  
  7.          left: '50%',  
  8.          textAlign: 'left',  
  9.          marginLeft: '-320px',  
  10.          marginTop: '-145px',  
  11.                  width: '600px',  
  12.                  background: 'none'  
  13.             }  
  14.         });  
  15.     $('.blockOverlay').attr('title','单击关闭').click($.unblockUI);   //遮罩层属性的设置以及当鼠标单击遮罩层可以关闭弹出层  
  16.     $('.close').click($.unblockUI);  //也可以自定义一个关闭按钮来关闭弹出层  
  17. });  
  18. });  

 

 

 

  1. jquery.blockUI.js  

 

 

  1. /*!  
  2.  * jQuery blockUI plugin  
  3.  * Version 2.33 (29-MAR-2010)  
  4.  * @requires jQuery v1.2.3 or later  
  5.  *  
  6.  * Examples at: http://malsup.com/jquery/block/  
  7.  * Copyright (c) 2007-2008 M. Alsup  
  8.  * Dual licensed under the MIT and GPL licenses:  
  9.  * http://www.opensource.org/licenses/mit-license.php  
  10.  * http://www.gnu.org/licenses/gpl.html  
  11.  *  
  12.  * Thanks to Amir-Hossein Sobhi for some excellent contributions!  
  13.  */  
  14.   
  15. ;(function($) {  
  16.   
  17. if (/1\.(0|1|2)\.(0|1|2)/.test($.fn.jquery) || /^1.1/.test($.fn.jquery)) {  
  18.     alert('blockUI requires jQuery v1.2.3 or later!  You are using v' + $.fn.jquery);  
  19.     return;  
  20. }  
  21.   
  22. $.fn._fadeIn = $.fn.fadeIn;  
  23.   
  24. var noOp = function() {};  
  25.   
  26. // this bit is to ensure we don't call setExpression when we shouldn't (with extra muscle to handle  
  27. // retarded userAgent strings on Vista)  
  28. var mode = document.documentMode || 0;  
  29. var setExpr = $.browser.msie && (($.browser.version < 8 && !mode) || mode < 8);  
  30. var ie6 = $.browser.msie && /MSIE 6.0/.test(navigator.userAgent) && !mode;  
  31.   
  32. // global $ methods for blocking/unblocking the entire page  
  33. $.blockUI   = function(opts) { install(window, opts); };  
  34. $.unblockUI = function(opts) { remove(window, opts); };  
  35.   
  36. // convenience method for quick growl-like notifications  (http://www.google.com/search?q=growl)  
  37. $.growlUI = function(title, message, timeout, onClose) {  
  38.     var $m = $('<div class="growlUI"></div>');  
  39.     if (title) $m.append('<h1>'+title+'</h1>');  
  40.     if (message) $m.append('<h2>'+message+'</h2>');  
  41.     if (timeout == undefined) timeout = 3000;  
  42.     $.blockUI({  
  43.         message: $m, fadeIn: 700, fadeOut: 1000, centerY: false,  
  44.         timeout: timeout, showOverlay: false,  
  45.         onUnblock: onClose,   
  46.         css: $.blockUI.defaults.growlCSS  
  47.     });  
  48. };  
  49.   
  50. // plugin method for blocking element content  
  51. $.fn.block = function(opts) {  
  52.     return this.unblock({ fadeOut: 0 }).each(function() {  
  53.         if ($.css(this,'position') == 'static')  
  54.             this.style.position = 'relative';  
  55.         if ($.browser.msie)  
  56.             this.style.zoom = 1; // force 'hasLayout'  
  57.         install(this, opts);  
  58.     });  
  59. };  
  60.   
  61. // plugin method for unblocking element content  
  62. $.fn.unblock = function(opts) {  
  63.     return this.each(function() {  
  64.         remove(this, opts);  
  65.     });  
  66. };  
  67.   
  68. $.blockUI.version = 2.33; // 2nd generation blocking at no extra cost!  
  69.   
  70. // override these in your code to change the default behavior and style  
  71. $.blockUI.defaults = {  
  72.     // message displayed when blocking (use null for no message)  
  73.     message:  '<h1>Please wait...</h1>',  
  74.   
  75.     title: null,      // title string; only used when theme == true  
  76.     draggable: true,  // only used when theme == true (requires jquery-ui.js to be loaded)  
  77.       
  78.     theme: false, // set to true to use with jQuery UI themes  
  79.       
  80.     // styles for the message when blocking; if you wish to disable  
  81.     // these and use an external stylesheet then do this in your code:  
  82.     // $.blockUI.defaults.css = {};  
  83.     css: {  
  84.         padding:    0,  
  85.         margin:     0,  
  86.         width:      '280px',  
  87.         top:        '50%',  
  88.         left:       '50%',  
  89.         textAlign:  'left',  
  90.         marginLeft:     '-140px',   
  91.         marginTop:      '-123px',   
  92.         color:      '#000',  
  93.         border:     'none',  
  94.         backgroundColor:'#fff',  
  95.         cursor:     'auto'  
  96.     },  
  97.       
  98.     // minimal style set used when themes are used  
  99.     themedCSS: {  
  100.         width:  '30%',  
  101.         top:    '40%',  
  102.         left:   '35%'  
  103.     },  
  104.   
  105.     // styles for the overlay  
  106.     overlayCSS:  {  
  107.         backgroundColor: '#000',  
  108.         opacity:         0.6,  
  109.         cursor:          'wait'  
  110.     },  
  111.   
  112.     // styles applied when using $.growlUI  
  113.     growlCSS: {  
  114.         width:      '350px',  
  115.         top:        '10px',  
  116.         left:       '',  
  117.         right:      '10px',  
  118.         border:     'none',  
  119.         padding:    '5px',  
  120.         opacity:    0.6,  
  121.         cursor:     'default',  
  122.         color:      '#fff',  
  123.         backgroundColor: '#000',  
  124.         '-webkit-border-radius': '10px',  
  125.         '-moz-border-radius':    '10px',  
  126.         'border-radius':         '10px'  
  127.     },  
  128.       
  129.     // IE issues: 'about:blank' fails on HTTPS and javascript:false is s-l-o-w  
  130.     // (hat tip to Jorge H. N. de Vasconcelos)  
  131.     iframeSrc: /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank',  
  132.   
  133.     // force usage of iframe in non-IE browsers (handy for blocking applets)  
  134.     forceIframe: false,  
  135.   
  136.     // z-index for the blocking overlay  
  137.     baseZ: 1000,  
  138.   
  139.     // set these to true to have the message automatically centered  
  140.     centerX: true, // <-- only effects element blocking (page block controlled via css above)  
  141.     centerY: true,  
  142.   
  143.     // allow body element to be stetched in ie6; this makes blocking look better  
  144.     // on "short" pages.  disable if you wish to prevent changes to the body height  
  145.     allowBodyStretch: true,  
  146.   
  147.     // enable if you want key and mouse events to be disabled for content that is blocked  
  148.     bindEvents: true,  
  149.   
  150.     // be default blockUI will supress tab navigation from leaving blocking content  
  151.     // (if bindEvents is true)  
  152.     constrainTabKey: true,  
  153.   
  154.     // fadeIn time in millis; set to 0 to disable fadeIn on block  
  155.     fadeIn:  200,  
  156.   
  157.     // fadeOut time in millis; set to 0 to disable fadeOut on unblock  
  158.     fadeOut:  400,  
  159.   
  160.     // time in millis to wait before auto-unblocking; set to 0 to disable auto-unblock  
  161.     timeout: 0,  
  162.   
  163.     // disable if you don't want to show the overlay  
  164.     showOverlay: true,  
  165.   
  166.     // if true, focus will be placed in the first available input field when  
  167.     // page blocking  
  168.     focusInput: true,  
  169.   
  170.     // suppresses the use of overlay styles on FF/Linux (due to performance issues with opacity)  
  171.     applyPlatformOpacityRules: true,  
  172.       
  173.     // callback method invoked when fadeIn has completed and blocking message is visible  
  174.     onBlock: null,  
  175.   
  176.     // callback method invoked when unblocking has completed; the callback is  
  177.     // passed the element that has been unblocked (which is the window object for page  
  178.     // blocks) and the options that were passed to the unblock call:  
  179.     //   onUnblock(element, options)  
  180.     onUnblock: null,  
  181.   
  182.     // don't ask; if you really must know: http://groups.google.com/group/jquery-en/browse_thread/thread/36640a8730503595/2f6a79a77a78e493#2f6a79a77a78e493  
  183.     quirksmodeOffsetHack: 4  
  184. };  
  185.   
  186. // private data and functions follow...  
  187.   
  188. var pageBlock = null;  
  189. var pageBlockEls = [];  
  190.   
  191. function install(el, opts) {  
  192.     var full = (el == window);  
  193.     var msg = opts && opts.message !== undefined ? opts.message : undefined;  
  194.     opts = $.extend({}, $.blockUI.defaults, opts || {});  
  195.     opts.overlayCSS = $.extend({}, $.blockUI.defaults.overlayCSS, opts.overlayCSS || {});  
  196.     var css = $.extend({}, $.blockUI.defaults.css, opts.css || {});  
  197.     var themedCSS = $.extend({}, $.blockUI.defaults.themedCSS, opts.themedCSS || {});  
  198.     msg = msg === undefined ? opts.message : msg;  
  199.   
  200.     // remove the current block (if there is one)  
  201.     if (full && pageBlock)  
  202.         remove(window, {fadeOut:0});  
  203.   
  204.     // if an existing element is being used as the blocking content then we capture  
  205.     // its current place in the DOM (and current display style) so we can restore  
  206.     // it when we unblock  
  207.     if (msg && typeof msg != 'string' && (msg.parentNode || msg.jquery)) {  
  208.         var node = msg.jquery ? msg[0] : msg;  
  209.         var data = {};  
  210.         $(el).data('blockUI.history', data);  
  211.         data.el = node;  
  212.         data.parent = node.parentNode;  
  213.         data.display = node.style.display;  
  214.         data.position = node.style.position;  
  215.         if (data.parent)  
  216.             data.parent.removeChild(node);  
  217.     }  
  218.   
  219.     var z = opts.baseZ;  
  220.   
  221.     // blockUI uses 3 layers for blocking, for simplicity they are all used on every platform;  
  222.     // layer1 is the iframe layer which is used to supress bleed through of underlying content  
  223.     // layer2 is the overlay layer which has opacity and a wait cursor (by default)  
  224.     // layer3 is the message content that is displayed while blocking  
  225.   
  226.     var lyr1 = ($.browser.msie || opts.forceIframe)   
  227.         ? $('<iframe class="blockUI" style="z-index:'+ (z++) +';display:none;border:none;margin:0;padding:0;position:absolute;width:100%;height:100%;top:0;left:0" src="'+opts.iframeSrc+'"></iframe>')  
  228.         : $('<div class="blockUI" style="display:none"></div>');  
  229.     var lyr2 = $('<div class="blockUI blockOverlay" style="z-index:'+ (z++) +';display:none;border:none;margin:0;padding:0;width:100%;height:100%;top:0;left:0"></div>');  
  230.       
  231.     var lyr3, s;  
  232.     if (opts.theme && full) {  
  233.         s = '<div class="blockUI blockMsg blockPage ui-dialog ui-widget ui-corner-all" style="z-index:'+z+';display:none;position:fixed">' +  
  234.                 '<div class="ui-widget-header ui-dialog-titlebar blockTitle">'+(opts.title || ' ')+'</div>' +  
  235.                 '<div class="ui-widget-content ui-dialog-content"></div>' +  
  236.             '</div>';  
  237.     }  
  238.     else if (opts.theme) {  
  239.         s = '<div class="blockUI blockMsg blockElement ui-dialog ui-widget ui-corner-all" style="z-index:'+z+';display:none;position:absolute">' +  
  240.                 '<div class="ui-widget-header ui-dialog-titlebar blockTitle">'+(opts.title || ' ')+'</div>' +  
  241.                 '<div class="ui-widget-content ui-dialog-content"></div>' +  
  242.             '</div>';  
  243.     }  
  244.     else if (full) {  
  245.         s = '<div class="blockUI blockMsg blockPage" style="z-index:'+z+';display:none;position:fixed"></div>';  
  246.     }             
  247.     else {  
  248.         s = '<div class="blockUI blockMsg blockElement" style="z-index:'+z+';display:none;position:absolute"></div>';  
  249.     }  
  250.     lyr3 = $(s);  
  251.   
  252.     // if we have a message, style it  
  253.     if (msg) {  
  254.         if (opts.theme) {  
  255.             lyr3.css(themedCSS);  
  256.             lyr3.addClass('ui-widget-content');  
  257.         }  
  258.         else   
  259.             lyr3.css(css);  
  260.     }  
  261.   
  262.     // style the overlay  
  263.     if (!opts.applyPlatformOpacityRules || !($.browser.mozilla && /Linux/.test(navigator.platform)))  
  264.         lyr2.css(opts.overlayCSS);  
  265.     lyr2.css('position', full ? 'fixed' : 'absolute');  
  266.   
  267.     // make iframe layer transparent in IE  
  268.     if ($.browser.msie || opts.forceIframe)  
  269.         lyr1.css('opacity',0.0);  
  270.   
  271.     //$([lyr1[0],lyr2[0],lyr3[0]]).appendTo(full ? 'body' : el);  
  272.     var layers = [lyr1,lyr2,lyr3], $par = full ? $('body') : $(el);  
  273.     $.each(layers, function() {  
  274.         this.appendTo($par);  
  275.     });  
  276.       
  277.     if (opts.theme && opts.draggable && $.fn.draggable) {  
  278.         lyr3.draggable({  
  279.             handle: '.ui-dialog-titlebar',  
  280.             cancel: 'li'  
  281.         });  
  282.     }  
  283.   
  284.     // ie7 must use absolute positioning in quirks mode and to account for activex issues (when scrolling)  
  285.     var expr = setExpr && (!$.boxModel || $('object,embed', full ? null : el).length > 0);  
  286.     if (ie6 || expr) {  
  287.         // give body 100% height  
  288.         if (full && opts.allowBodyStretch && $.boxModel)  
  289.             $('html,body').css('height','100%');  
  290.   
  291.         // fix ie6 issue when blocked element has a border width  
  292.         if ((ie6 || !$.boxModel) && !full) {  
  293.             var t = sz(el,'borderTopWidth'), l = sz(el,'borderLeftWidth');  
  294.             var fixT = t ? '(0 - '+t+')' : 0;  
  295.             var fixL = l ? '(0 - '+l+')' : 0;  
  296.         }  
  297.   
  298.         // simulate fixed position  
  299.         $.each([lyr1,lyr2,lyr3], function(i,o) {  
  300.             var s = o[0].style;  
  301.             s.position = 'absolute';  
  302.             if (i < 2) {  
  303.                 full ? s.setExpression('height','Math.max(document.body.scrollHeight, document.body.offsetHeight) - (jQuery.boxModel?0:'+opts.quirksmodeOffsetHack+') + "px"')  
  304.                      : s.setExpression('height','this.parentNode.offsetHeight + "px"');  
  305.                 full ? s.setExpression('width','jQuery.boxModel && document.documentElement.clientWidth || document.body.clientWidth + "px"')  
  306.                      : s.setExpression('width','this.parentNode.offsetWidth + "px"');  
  307.                 if (fixL) s.setExpression('left', fixL);  
  308.                 if (fixT) s.setExpression('top', fixT);  
  309.             }  
  310.             else if (opts.centerY) {  
  311.                 if (full) s.setExpression('top','(document.documentElement.clientHeight || document.body.clientHeight) / 2 - (this.offsetHeight / 2) + (blah = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + "px"');  
  312.                 s.marginTop = 0;  
  313.             }  
  314.             else if (!opts.centerY && full) {  
  315.                 var top = (opts.css && opts.css.top) ? parseInt(opts.css.top) : 0;  
  316.                 var expression = '((document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + '+top+') + "px"';  
  317.                 s.setExpression('top',expression);  
  318.             }  
  319.         });  
  320.     }  
  321.   
  322.     // show the message  
  323.     if (msg) {  
  324.         if (opts.theme)  
  325.             lyr3.find('.ui-widget-content').append(msg);  
  326.         else  
  327.             lyr3.append(msg);  
  328.         if (msg.jquery || msg.nodeType)  
  329.             $(msg).show();  
  330.     }  
  331.   
  332.     if (($.browser.msie || opts.forceIframe) && opts.showOverlay)  
  333.         lyr1.show(); // opacity is zero  
  334.     if (opts.fadeIn) {  
  335.         var cb = opts.onBlock ? opts.onBlock : noOp;  
  336.         var cb1 = (opts.showOverlay && !msg) ? cb : noOp;  
  337.         var cb2 = msg ? cb : noOp;  
  338.         if (opts.showOverlay)  
  339.             lyr2._fadeIn(opts.fadeIn, cb1);  
  340.         if (msg)  
  341.             lyr3._fadeIn(opts.fadeIn, cb2);  
  342.     }  
  343.     else {  
  344.         if (opts.showOverlay)  
  345.             lyr2.show();  
  346.         if (msg)  
  347.             lyr3.show();  
  348.         if (opts.onBlock)  
  349.             opts.onBlock();  
  350.     }  
  351.   
  352.     // bind key and mouse events  
  353.     bind(1, el, opts);  
  354.   
  355.     if (full) {  
  356.         pageBlock = lyr3[0];  
  357.         pageBlockEls = $(':input:enabled:visible',pageBlock);  
  358.         if (opts.focusInput)  
  359.             setTimeout(focus, 20);  
  360.     }  
  361.     else  
  362.         center(lyr3[0], opts.centerX, opts.centerY);  
  363.   
  364.     if (opts.timeout) {  
  365.         // auto-unblock  
  366.         var to = setTimeout(function() {  
  367.             full ? $.unblockUI(opts) : $(el).unblock(opts);  
  368.         }, opts.timeout);  
  369.         $(el).data('blockUI.timeout', to);  
  370.     }  
  371. };  
  372.   
  373. // remove the block  
  374. function remove(el, opts) {  
  375.     var full = (el == window);  
  376.     var $el = $(el);  
  377.     var data = $el.data('blockUI.history');  
  378.     var to = $el.data('blockUI.timeout');  
  379.     if (to) {  
  380.         clearTimeout(to);  
  381.         $el.removeData('blockUI.timeout');  
  382.     }  
  383.     opts = $.extend({}, $.blockUI.defaults, opts || {});  
  384.     bind(0, el, opts); // unbind events  
  385.       
  386.     var els;  
  387.     if (full) // crazy selector to handle odd field errors in ie6/7  
  388.         els = $('body').children().filter('.blockUI').add('body > .blockUI');  
  389.     else  
  390.         els = $('.blockUI', el);  
  391.   
  392.     if (full)  
  393.         pageBlock = pageBlockEls = null;  
  394.   
  395.     if (opts.fadeOut) {  
  396.         els.fadeOut(opts.fadeOut);  
  397.         setTimeout(function() { reset(els,data,opts,el); }, opts.fadeOut);  
  398.     }  
  399.     else  
  400.         reset(els, data, opts, el);  
  401. };  
  402.   
  403. // move blocking element back into the DOM where it started  
  404. function reset(els,data,opts,el) {  
  405.     els.each(function(i,o) {  
  406.         // remove via DOM calls so we don't lose event handlers  
  407.         if (this.parentNode)  
  408.             this.parentNode.removeChild(this);  
  409.     });  
  410.   
  411.     if (data && data.el) {  
  412.         data.el.style.display = data.display;  
  413.         data.el.style.position = data.position;  
  414.         if (data.parent)  
  415.             data.parent.appendChild(data.el);  
  416.         $(el).removeData('blockUI.history');  
  417.     }  
  418.   
  419.     if (typeof opts.onUnblock == 'function')  
  420.         opts.onUnblock(el,opts);  
  421. };  
  422.   
  423. // bind/unbind the handler  
  424. function bind(b, el, opts) {  
  425.     var full = el == window, $el = $(el);  
  426.   
  427.     // don't bother unbinding if there is nothing to unbind  
  428.     if (!b && (full && !pageBlock || !full && !$el.data('blockUI.isBlocked')))  
  429.         return;  
  430.     if (!full)  
  431.         $el.data('blockUI.isBlocked', b);  
  432.   
  433.     // don't bind events when overlay is not in use or if bindEvents is false  
  434.     if (!opts.bindEvents || (b && !opts.showOverlay))   
  435.         return;  
  436.   
  437.     // bind anchors and inputs for mouse and key events  
  438.     var events = 'mousedown mouseup keydown keypress';  
  439.     b ? $(document).bind(events, opts, handler) : $(document).unbind(events, handler);  
  440.   
  441. // former impl...  
  442. //     var $e = $('a,:input');  
  443. //     b ? $e.bind(events, opts, handler) : $e.unbind(events, handler);  
  444. };  
  445.   
  446. // event handler to suppress keyboard/mouse events when blocking  
  447. function handler(e) {  
  448.     // allow tab navigation (conditionally)  
  449.     if (e.keyCode && e.keyCode == 9) {  
  450.         if (pageBlock && e.data.constrainTabKey) {  
  451.             var els = pageBlockEls;  
  452.             var fwd = !e.shiftKey && e.target == els[els.length-1];  
  453.             var back = e.shiftKey && e.target == els[0];  
  454.             if (fwd || back) {  
  455.                 setTimeout(function(){focus(back)},10);  
  456.                 return false;  
  457.             }  
  458.         }  
  459.     }  
  460.     // allow events within the message content  
  461.     if ($(e.target).parents('div.blockMsg').length > 0)  
  462.         return true;  
  463.   
  464.     // allow events for content that is not being blocked  
  465.     return $(e.target).parents().children().filter('div.blockUI').length == 0;  
  466. };  
  467.   
  468. function focus(back) {  
  469.     if (!pageBlockEls)  
  470.         return;  
  471.     var e = pageBlockEls[back===true ? pageBlockEls.length-1 : 0];  
  472.     if (e)  
  473.         e.focus();  
  474. };  
  475.   
  476. function center(el, x, y) {  
  477.     var p = el.parentNode, s = el.style;  
  478.     var l = ((p.offsetWidth - el.offsetWidth)/2) - sz(p,'borderLeftWidth');  
  479.     var t = ((p.offsetHeight - el.offsetHeight)/2) - sz(p,'borderTopWidth');  
  480.     if (x) s.left = l > 0 ? (l+'px') : '0';  
  481.     if (y) s.top  = t > 0 ? (t+'px') : '0';  
  482. };  
  483.   
  484. function sz(el, p) {  
  485.     return parseInt($.css(el,p))||0;  
  486. };  
  487.   
  488. })(jQuery);  



 

 

看了上面的代码,其实你会发觉BlockUI的使用很简单,有2个重要的方法分别是调用弹出层($.blockUI)和关闭弹出层($.unblockUI)。

$.blockUI定义了一个message属性的,该属性的值就定义了要弹出的元素,比如要弹出一个id为box的div元素,就可以这样写:message: $(‘#box’),对应了上面的第4行代码。需要注意的是,要弹出的那个元素在弹出之前要将其在页面中隐藏即设置该元素的CSS样式为display:none。

$.blockUI还定义了一个css属性,该属性可以对弹出层的样式进行再定义。细心的同志可能会打开BlockUI的库文件查看,其实在库文件中也默认定义了一个弹出层的样式,如果你在页面中定义的样式效果并不是很理想,最好是看看库文件中的$.blockUI.defaults的CSS属性。

如果你想了解一些BlockUI的更高级的用法你可以去该插件的官网看看说明文档

BlockUI的下载地址是:hhttp://jquery.malsup.com/block/#download

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics