【发布时间】:2014-03-17 21:30:14
【问题描述】:
我的 jQuery 有问题:我使用的脚本允许我通过给元素一个类来居中元素,但是这个脚本没有采用正确的高度。
这是我的 HTML 代码:
<div class="logoutscreen blackbackground">
<div class="window centered" style="display: block;">
[CONTENT HERE]
</div>
</div>
这是我的 jQuery 代码:
$(function () {
$(".centered").css({
'position': 'absolute',
'left': '50%',
'top': '50%',
'margin-left': -$(this).outerWidth() / 2,
'margin-top': -$(this).outerHeight() / 2
});
});
问题在于脚本不采用具有 .centered 类 (.window) 的 div 的高度和宽度,而是采用其父级 (.logoutscreen) 的高度和宽度。
为什么会这样? :(
【问题讨论】:
标签: javascript jquery html css