关于如何改进此"Vertical-align Element Tag"代码的任何建议?



这段代码首先计算窗口的高度,然后通过对所选元素的父元素应用padding来使Element居中。

我只是认为代码是相当冗长的,我希望有一个更好的方法来实现这个目标,而不完全重写代码!?

$(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');
});
$(function(){
 "use strict";
 $(boxSlector).css({"position":"fixed","top":"50%","transform":"translateY(-50%)"});
});

最新更新