如何在具有位置的任何分辨率下以精確的屏幕中心調整div:絕對。調整div位置
Q
調整div位置
-2
A
回答
4
如果div有一個已知的寬度和高度,你可以使用負保證金:
div {
position: absolute;
width: 800px;
height: 500px;
left: 50%;
top: 50%;
margin-top: -250px;
margin-left: -400px;
}
通知負利潤的寬度和高度的一半。
如果div的大小不知道或流體,你必須使用JavaScript。以下是如何使用jQuery進行工作:
$(function() {
$(window).resize(function() {
$('div.something').css({
left: $(window).width() - ($(this).width()/2),
top: $(window).height() - ($(this).height()/2)
});
});
$(window).resize();
});
+0
請注意,這將(在小窗口中)將內容放置在窗口之外而不使其可用滾動條訪問。 – Quentin 2009-12-07 12:53:18
1
以下幾段腳本將爲您提供窗口中可視空間的高度和寬度。
<script type="text/javascript">
<!--
function pageWidth() {
return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}
function pageHeight() {
return window.innerHeight != null ? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null;
}
function posLeft() {
return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}
function posTop() {
return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}
function posRight() {
return posLeft() + pageWidth();
} function posBottom() {
return posTop() + pageHeight();
}
pageWidth()
pageHeight()
//-->
</script>
簡單的數學的一點點就會讓你的屏幕的中心x =寬度/ 2 Y =身高/ 2
設置頂部位置爲y +的xy座標(DivHeight/2),潛水的左側位置X-(DivWidth/2)
0
selector {
position: absolute;
width: 100px;
height: 100px;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -50px;
}
相關問題
- 1. 調整位置絕對div到中間?
- 2. CSS位置調整
- 3. 調整後調整UIPopoverController位置
- 4. 調整div div
- 5. CSS圖像位置調整
- 6. 調整UITabBarItem徽章位置?
- 7. 端點位置調整
- 8. div定位調整高度和寬度
- 9. DOJO調整div div
- 10. 調整大小DIV調整
- 11. 在調整窗口大小時將div改爲相同位置
- 12. jquery:自動調整絕對位置的子div
- 13. 在調整大小的同時保持div中心位置
- 14. 在調整大小css上更改div位置
- 15. 如何根據鼠標位置調整div的大小?
- 16. 滾動div,自動調整高度和位置
- 17. 用相對位置div重新調整大小
- 18. 調整窗口大小並將div保持在同一位置
- 19. 在一個div中的循環div工作,但調整位置不
- 20. 調整div
- 21. 如何在調整屏幕大小時將div放置在中心位置?
- 22. 爲什麼div的位置設置爲固定時會調整大小?
- 23. 在div中定位和調整div元素的大小
- 24. Div的位置
- 25. HTML div位置
- 26. 屏幕大小調整和元素位置調整
- 27. 如何調整調整UICollectionView大小的Autolayout位置?
- 28. 調整jpanel在jframe上的位置調整大小
- 29. 調整div元素
- 30. 調整兩個div?
中心屏幕?或瀏覽器窗口的中心? – peirix 2009-12-07 13:08:46