I write a plugin for this jquery plugin.
Well more to perfect.
Please notice how to gain 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;
})();