简体   繁体   English

使用jQuery使元素填充浏览器视口的整个高度

[英]Making an element fill the entire height of browser viewport using jQuery

Code
Demo 演示

The basic form of HTML looks like this: HTML的基本形式如下所示:

<div class="home">
    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">

            <!-- blah, blah, blah! -->

        </main>
    </div>
</div>

Wrt the HTML, I am trying to make the element #main fill the entire height of the browser viewport using JavaScript/jQuery like so: 使用HTML,我正在尝试使用JavaScript / jQuery使元素#main填充浏览器视口的整个高度,如下所示:

jQuery(document).ready(function($){

    // get height of browser viewport
    var window_h = $(window).height();

    // get height of the jumbotron, i.e. element #main
    var jumbotron_h = $('.home #main').outerHeight(true);

    // calculate necessary padding (top/bottom) to apply on #main so that the
    // element's height is equal to that of the browser's viewport,
    // and its contents are centered vertically
    if (window_h > (jumbotron_h + 60)) {
        var jumbotron_padding = (window_h - jumbotron_h)/2
    } else {
        var jumbotron_padding = 30
    }

    // apply calculated padding on the element dynamically
    $('.home #main').attr('style', 'padding-top:'+jumbotron_padding+'px;padding-bottom:'+jumbotron_padding+'px;');

});

As clearly explained in the comments in the code above, the code automatically calculates the necessary padding to be applied on #main so that its height is equal to that of the browser's viewport. 如上面代码中的注释所清楚解释的那样,该代码自动计算要应用于#main上的必要填充,以使其高度等于浏览器视口的高度。

It works well, except, the calculated padding (and therefore the resultant height) is wrong in one case that I was able to identify. 它工作得很好,除了在我能够确定的一种情况下,计算出的填充(以及因此得到的高度)错误之外。

Easily reproducible at least on Windows 7, Google Chrome browser (latest) when you resize the browser window to 567x724 px, which implies 551x611 px viewport size, (you can use an extension like Window Resizer ), you'll notice that the element's calculated padding results in its height being larger than that of the browser's viewport. 至少Windows 7,Google Chrome浏览器 (最新)上可以轻松重现将浏览器窗口的大小调整为567x724 px(这意味着551x611 px的视口大小)(可以使用Window Resizer之类的扩展名),您会注意到该元素的计算方式填充会导致其高度大于浏览器视口的高度。

Why is this happening? 为什么会这样呢? I wasn't able to reproduce the same at any other resolution. 我无法以其他任何分辨率重现相同的图像。 What could I possibly be missing here? 我在这里可能会想念什么?

First off, Jquery's .outerheight() function includes padding, which means that when you measure the height of your #Main element after this function runs the first time, it will equal the window.height . 首先,Jquery的.outerheight()函数包含 padding,这意味着当此函数首次运行后测量#Main元素的高度时,它将等于window.height In other words - it will look awful when you resize your browser. 换句话说,当您调整浏览器大小时,它看起来很糟糕。 You can see this in action on this fiddle when you resize the browser window the old-fashioned way. 当您以老式方式调整浏览器窗口的大小时,可以在此小提琴上看到这一点。 You can use margins instead, but then you'll have to adjust your CSS quite a bit. 您可以改用边距,但随后必须对CSS进行大量调整。 Even then, resizing the window still looks awful and buggy and has inconsistent results across browsers. 即使那样,调整窗口的大小仍然看起来很糟糕且有问题,并且在浏览器之间的结果不一致。 You can see that on this fiddle . 您可以在小提琴上看到这一点

The specific bug you're referring to is probably due to inconsistent math when you resize your window - a combination of your use of padding (which is included in .outerheight() as mentioned above) and the viewport size not being easily divisible by 2 (it's impossible to have half a pixel and different browsers will render that half a pixel differently). 您所指的特定错误可能是由于调整窗口大小时的数学不一致-结合使用填充(如上所述,该填充在.outerheight()中)和视口大小不易被2整除的组合(不可能有一个半像素,不同的浏览器将以不同的方式渲染该半像素)。

I should also point out this line of code: 我还应该指出这一行代码:

if (window_h > (jumbotron_h + 60)) {
    var jumbotron_padding = (window_h - jumbotron_h)/2
} else {
    var jumbotron_padding = 30
}

This forces your page to always be #main.height() + 60 , which can be bigger than your viewable window, depending upon your window size. 这将强制您的页面始终为#main.height() + 60 ,它可能大于您的可见窗口,具体取决于您的窗口大小。 #main.height() comes out to around 200.5px (there we are with another half pixel). #main.height()约200.5像素(我们还有另一个半像素)。

Assuming that your goal is to vertically center the #Main element, your best bet is to use one of the many straight CSS methods available. 假设您的目标是垂直#Main元素居中, #Main最好的选择是使用许多可用的直接CSS方法之一。 The table method seems most applicable here because it is completely dynamic (and thus can be responsive) - simply create a single cell CSS table, use CSS valign, and build your entire page inside that cell. table方法在这里似乎最适用,因为它是完全动态的(因此可以响应)-只需创建一个单元格CSS表,使用CSS valign,然后在该单元格内构建整个页面。 For older versions of IE you'll need to use a tiny hack ( display:Inline-block; ) to make it work. 对于较旧版本的IE,您需要使用一个小小的hack( display:Inline-block; )使其正常工作。

HTML: HTML:

<div id="parent">
    <div id="child">Content here</div>
</div>

CSS: CSS:

#parent {display: table;}

#child {
    display: table-cell;
    vertical-align: middle;
}

IE fix: IE修复:

#child {
    display: inline-block;
}

Based on @Thomas's answer, I've identified two possible solutions. 根据@Thomas的回答,我确定了两种可能的解决方案。 I am going with the #2 solution considering better browser support for it. 考虑使用更好的浏览器支持,我将使用第二种解决方案。


1. Using the unit vh (viewport height). 1.使用单位vh (视口高度)。 Browser support : IE9 and above. 浏览器支持 :IE9及更高版本。

CSS: CSS:

.home #primary { /* Parent */
    display: table;
    width: 100%;
}

.home #main { /* Child */
    display: table-cell;
    height: 100vh; /* 100% viewport height */
    vertical-align: middle;
}

2. Dynamically setting height of parent ( .home #primary ) equal to that of browser's viewport. 2.动态设置父级( .home #primary )的高度等于浏览器视口的高度。

JS: JS:

jQuery(document).ready(function($){

    var window_h = $(window).height();
    var jumbotron_h = $('.home #main').height();

    if (window_h <= (jumbotron_h + 60)) {
        var window_h = jumbotron_h + 60
    }

    $('.home #primary').attr('style', 'height:'+window_h+'px;');

});

CSS: CSS:

.home #primary { /* Parent */
    display: table;
    width: 100%;
}

.home #main { /* Child */
    display: table-cell;
    height: 100%; /* 100% height of parent */
    vertical-align: middle;
}
       body {
        height:100vh;
       }

       .element-to-be-centered { 
        font-size:1em; 
        text-align:center; 
        top: 49%;
        -webkit-transform: translateY(-49%);
        -ms-transform: translateY(-49%);
        transform: translateY(-49%);
       }

I have been doing quite a bit of experimenting lately with centering content. 最近,我一直在做很多关于居中内容的实验。 This technique will work without having to set a height on any element. 无需在任何元素上设置高度即可使用此技术。

This will work in ie9 and up. 这将在ie9及更高版本中起作用。

In your CSS 在您的CSS中

html, body {
    height: 100%;
}

div, #main {
    height: 100%;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM