【发布时间】:2014-02-25 07:46:38
【问题描述】:
我正在尝试使我的部门垂直居中。
我需要一些东西来告诉'margin-top'到 -50% 的部门。
你可以在这里看到它:http://jsfiddle.net/N5gsW/
我知道我做错了什么:(
谢谢
HTML
<body>
<div id="content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent iaculis quam non massa venenatis malesuada. Ut mollis, nibh id placerat euismod, magna lectus sodales lacus, nec ultrices.
</div>
CSS
body{
margin: 0 0 0 0;
width: 100%;
height: 100%;
padding: 0 0 0 0;
}
#content{
width: 100px;
left: 50%;
margin-left: -50px;
top: 50%;
position: fixed;
background: #0C6;
}
Javascript
$(document).ready(function() {
$('#content').each(function() {
var parentHeight = $(this).parent().height();
var thisHeight = $(this).height();
var topmargin = (parentHeight - thisHeight) / 2;
$(this).css("margin-top", topmargin);
});
})
【问题讨论】:
-
已编辑,不知道,抱歉。
标签: javascript alignment center