您必须以这种方式将其与屏幕中心对齐:
left = ( this.parent().width() / 2 ) - (this.width() / 2 );
top = ( this.parent().height/ 2 ) - (this.height() / 2 );
this.css({top:top,left:left});
使用自爆 jQuery 插件,并调用
<script type="text/javascript" charset="utf-8">
$("#loading").ajaxStart(function(){
$(this).show().align(); // for absolute position pass {position:'absolute'}
}).ajaxStop(function(){
$(this).hide();
});
</script>
您还可以在身体上创建叠加层$('body').overlay();
// 使用这段代码:
;(function($){
$.extend($.fn,{
// Overlay ; $(body).overlay();
overlay: function(o){
var d={
addClass:'',
css:{},
opacity:0.5,
autoOpen:true,
zIndex:998,
onOpen:function(){x.show();},
onClose:function(){x.hide();},
onRemove:function(){x.remove();}
},x,l;
// Merge Options
o=$.extend({},d,o);
// Create overlay element
x=$('<div>',{css:{opacity:o.opacity,zIndex:o.zIndex}}).css(o.css).addClass('overlay '+o.addClass).hide().appendTo(this);
// If appended to body element, set position fixed
this[0].tagName.toLowerCase()=="body"&&x.css("position","fixed");
// Overlay Control Object
l = {
remove:function(){o.onRemove(l)},
open:function(){o.onOpen(l)},
close:function(){o.onClose(l)},
obj:x
};
// Call On Show
o.autoOpen&& o.onOpen(l);
// Return Object
return l;
},
align:function(o){
var d={
x:'center',
y:'center',
position:'fixed',
parent:window
};
o=$.extend({},d,o);
var c=$(o.parent),
t=$(this),
s=c.scrollTop(),
top,
left;
t.css('position',o.position); // Set Css Position
if(o.y)top = o.y=='center' ? (c.height()/2) - (t.innerHeight()/2) : o.y;
if(o.x)left = o.x=='center' ? (c.width()/2) - (t.innerWidth()/2) : o.x;
if(o.position=='absolute') top += s; // For absolute position
if(top<0) top=0;
t.css({top:top,left:left});
},
});
})(jQuery);
覆盖的CSS:
html{height:100%;width:100%;}
.overlay{width:100%;height:100%;top:0;right:0;position:absolute;background:#000;}