我创建了一个函数来使用jQuery来集中我的<div>
,它在页面加载时工作,但不在页面调整大小。为什么我的函数不能在.resize()上运行?
我在做什么错?
JS:
<script>
$(document).ready(function() {
function reCenter() {
$('.content').css({
position:'absolute',
left: ($(window).width()
- $('.content').outerWidth())/2,
top: ($(window).height()
- $('.content').outerHeight())/2
});
}
$(window).resize(reCenter());
// To initially run the function:
reCenter();
});
</script>
HTML:
<div class="content">
<h1>Header</h1>
<span class="hyphen">-</span>
<h2>Subtext</h2>
</div>
CSS:
* {
padding: 0;
margin: 0;
}
html {
background: darkgrey;
}
body {
color: #fff;
text-align: center;
/*padding-top: 300px;*/
}
h1 {
font-family: 'Arimo', sans-serif;
text-transform: uppercase;
font-weight: bold;
font-size: 40px;
}
span.hyphen {
color: #0CFFFC;
font-size: 3em;
}
h2 {
font-family: 'Montserrat', sans-serif;
font-weight: 100;
}
UPDATE
让网页加载火起来的功能后,它的页面加载和窗口之间存在一些不一致的行为调整 http://jsfiddle.net/7zqkd4w8/
如果您的内容不是动态的,你可以尝试使用方法[这里](http://www.wpdfd.com/editorial/thebox/deadcentre4.html),其中中心块垂直和水平 – yoavmatchulsky 2014-11-09 08:58:43