此代码首先计算窗口的高度,然后通过将填充应用于所选元素的父元素,将元素置于窗口中心。
我只是认为代码非常详细,我希望有更好的方法来实现这个目标,而不是完全重写代码!
$(function(){
"use strict";
var wheight = $(window).height();
var programs = {
fullScreen : function(stretch) {
$(stretch).css('height', wheight);
},//fullScreen Method
centerBoxVertical : function(boxSlector) {
var boxHeight = $(boxSlector).height(),
paddingTop = parseFloat($(boxSlector).css('padding-top')),
paddingbottom = parseFloat($(boxSlector).css('padding-bottom')),
totalBoxHeight = boxHeight + paddingTop + paddingbottom,
newPaddingTopBottom = (wheight - totalBoxHeight)/2;
$(boxSlector).parent().css('padding-top',newPaddingTopBottom);
}//centerBoxVertical Method
}// programs Object
programs.fullScreen('.fullheight')
programs.centerBoxVertical('#rooms .room .content');
programs.centerBoxVertical('.fullheight .hgroup');
});