通过使用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();
});
答案 0 :(得分:1)
看起来你正在调整resize处理程序中的$(window).resize()
$(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")
});
}).resize(); // To initially run the function:
});