div absolute 居中

很多情况下,我们需要在网页上弹出dialog,需要水平垂直居中。而且可以做到自动适应窗口变化

对于页面绝对定位,垂直居中
var h = $(this).height();  
var oh = $(this).outerHeight();  
var mt = (h + (oh - h)) / 2;  
$(this).css("margin-top", "-" + mt + "px");  
$(this).css("top", "50%");  
$(this).css("position", "absolute");  


对于页面绝对定位,水平居中
var w = $(this).width();  
var ow = $(this).outerWidth();    
var ml = (w + (ow - w)) / 2;      
$(this).css("margin-left", "-" + ml + "px");  
$(this).css("left", "50%");  
$(this).css("position", "absolute");  

原文链接地址: http://www.jquery001.com/jquery-plugin-center-object-exactly.html

猜你喜欢

转载自yudey.iteye.com/blog/1759418