2011-04-20 46 views
1

我需要在屏幕中間添加一個覆蓋框。我必須留在那裏,直到我將其刪除。即使頁面滾動,我也應該保持在中間。我的猜測我需要一個相對定位,但我如何獲得利潤率等。我是否需要使用jQuery來計算它?屏幕中間的位置div w/jQuery

$(".myBox").css("margin-top", $(document).height()/2 - $(".myBox").height()/2); 
+0

你的代碼有什麼問題? – sdleihssirhc 2011-04-20 15:43:33

回答

0

你所尋找的是position:fixed(不是IE < 7支持)。 https://developer.mozilla.org/en/CSS/position滿足此要求:「即使頁面滾動,它也應保持在中間。」

$(".myBox").css("top", $(document).height()/2 - $(".myBox").height()/2); 
$(".myBox").css("position", "fixed"); 
1

如果你有你的DIV指定寬度和高度,然後你可以使用(CSS代碼):

margin-left: auto; 
margin-top: auto; 

,或者如果你想這樣做jQuery中:

$(".myBox").css("margin-top", "auto"); 
$(".myBox").css("margin-left", "auto"); 

這應該由IE以及Firefox,Opera,Safari等支持。