I am writing a plugin for this jquery plugin. Well, even better.
Pay attention to how to get the center of height.
/ **
* added by lijg.
* refer: http://forum.jquery.com/topic/blockui-centering-the-dialog-window
* /
(function () {
function cp_props (inSrcObj, inDestObj, inOverride) {
if (inDestObj == null) {
return inSrcObj;
}
var prop;
for (prop in inSrcObj) {
if (inOverride ||! inDestObj [prop]) {// 先 判断 是否 可以 重写 , true 则 不必 在 计算 后面 的 值 , false 在 计算 后面 时候 存在 这个 属性。
inDestObj [prop] = inSrcObj [prop];
}
}
return inDestObj;
}
function _debug_info (container, aim) {
console.info ("$ (window) .height ():" + $ (window) .height () + ", $ (window) .width ():" + $ (window) .width ());
console.info ("$ (window) .scrollTop ():" + $ (window) .scrollTop () + ", $ (window) .scrollLeft ():" + $ (window) .scrollLeft ());
console.info ("container.height ():" + container.height () + ", container.width ():" + container.width ());
}
function center_of_dom (container, aim) {
// _ debug_info (container, aim);
container.css ("position", "absolute");
container.css ("width", aim.width () + "px");
container.css ("height", aim.height () + "px");
container.css ("top", ($ (window) .height () - container.height ()) / 2 + $ (window) .scrollTop () + "px");
container.css ("left", ($ (window) .width () - container.width ()) / 2 + $ (window) .scrollLeft () + "px");
}
function center_of_x (container, aim) {
// _ debug_info (container, aim);
container.css ("position", "absolute");
container.css ("width", aim.width () + "px");
container.css ("left", ($ (window) .width () - container.width ()) / 2 + $ (window) .scrollLeft () + "px");
}
function center_of_y (container, aim) {
// _ debug_info (container, aim);
container.css ("position", "absolute");
container.css ("height", aim.height () + "px");
container.css ("top", ($ (window) .height () - container.height ()) / 2 + $ (window) .scrollTop () + "px");
}
$ ._ blockUI = $ .blockUI;
$ .blockUI = function (opts) {
if (! opts.onOverlayClick) {
opts.onOverlayClick = $ .unblockUI;
}
$ ._ blockUI (opts); // call blockUI
var container = $ ('. blockUI.blockMsg');
var aim = opts.message;
if (opts.auto_center) {
center_of_dom (container, aim); // center it
// center when resize
$ (window) .resize (function () {
center_of_dom (container, aim);
});
} else if (opts.auto_center_x) {
center_of_x (container, aim); // center it
// center when resize
$ (window) .resize (function () {
center_of_x (container, aim);
});
} else if (opts.auto_center_y) {
center_of_y (container, aim); // center it
// center when resize
$ (window) .resize (function () {
center_of_y (container, aim);
});
}
};
cp_props ($ ._ blockUI, $ .blockUI, true); // cp properties
// other methods
$ .blockUI.center_of_dom = center_of_dom;
}) ();
source share