【发布时间】:2013-11-26 05:22:06
【问题描述】:
通过使用 jquery,我将 div 放置在窗口的垂直中心。此脚本不支持 $(document).ready(function() 但它仅在窗口调整大小功能后才开始工作。
我需要它在第一次加载页面时工作。参考fiddle
HTML:
<div class="welcomer">
<center>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</center>
</div>
CSS:
body {
background: #ccc;
color:#000;
}
.welcomer {
width: 100%;
background: #fff;
}
jQuery
$(document).ready(function(){
$(window).resize(function(){
$('.welcomer').css({
position:'absolute',
"top": ((($(window).height() - $('.welcomer').outerHeight()) / 2) + $(window).scrollTop() + "px"),
"left": ((($(window).width() - $('.welcomer').outerWidth()) / 2) + $(window).scrollLeft() + "px")
});
// To initially run the function:
$(window).resize();
});
【问题讨论】: