简体   繁体   English

全高div到屏幕尺寸-JQuery

[英]full height div to screen size - JQuery

I have created a page where I am calculating the height of header and footer height and window height, and applying the remaining height to content area so the content area covers the and fill it. 我创建了一个页面,用于计算页眉和页脚的高度以及窗口的高度,然后将剩余的高度应用于内容区域,以便内容区域覆盖并填充它。

It's not working as expected, I am getting scroll on the page which usually shouldn't be, scroll should appear only when the content of the body is more than to the remaining space. 它没有按预期工作,我在通常不应该在该页面上滚动的页面,只有当正文内容超出剩余空间时,滚动页面才会出现。

Here is the JSfiddle 这是JSfiddle

 function contentHeight() { var winH = $(window).height(), headerHei = $("header").height(), footerHei = $("footer").height(), contentHei = winH - headerHei - footerHei; $(".content-wrapper").css("min-height", contentHei); } $(document).ready(function () { contentHeight(); }); $(window).resize(function () { contentHeight(); }); 
 *, ::after, ::before { box-sizing: border-box } header{float:left; width:100%; padding;10px 0; background:#ececec;} .content-wrapper{float:left; width:100%; padding:20px; background:#cdcdcd;} footer{float:left; width:100%; padding:15px 0; background:#333333;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <header><p>This is heading</p><p>This is sub-heading</p></header> <div class="content-wrapper"> <p> this is content area </p> <p> this is content area </p> </div> <footer><p>This is footer</p></footer> 
EDIT: Screenshot of chrome 编辑:chrome的屏幕截图

The reason for your issue is you have used .height() to measure height of the element, but they have top and bottom padding and .height() don't consider padding. 问题的原因是您已经使用.height()来测量元素的高度,但是它们具有顶部和底部填充,而.height()则不考虑填充。 You should try .outerHeight() . 您应该尝试.outerHeight() Try this code. 试试这个代码。

function contentHeight() {
  var winH = $(window).outerHeight(),
    headerHei = $("header").outerHeight(),
    footerHei = $("footer").outerHeight(),
    contentHei = winH - headerHei - footerHei;
  $(".content-wrapper").css("min-height", contentHei);
}

$(document).ready(function() {
  contentHeight();
});
$(window).resize(function() {
  contentHeight();
});

You can also consider the reference for .height and .outerHeight here. 您还可以在此处考虑.height.outerHeight的引用。 https://www.texelate.co.uk/blog/jquery-whats-the-difference-between-height-innerheight-and-outerheight https://www.texelate.co.uk/blog/jquery-whats-the-difference-between-height-innerheight-and-outerheight

probably a simple: 可能很简单:

body{
  margin:0;
}

could fix everything. 可以解决所有问题。

OR 要么

Well, as it seems to be not calculating very well, you can try to 'bypass' this problem getting the paddings (that seems to be the problem over here). 好吧,由于似乎计算得不太好,您可以尝试“跳过”获取填充物的问题(这似乎是这里的问题)。

Then you can set the content height beign: 然后,您可以设置内容高度beign:
total height - (footer + header + paddings) 总高度-(页脚+页眉+填充)

My code below do this, see if this helps you. 我下面的代码可以做到这一点,看看是否有帮助。 (open snippet in full page size) (以完整页面大小打开的代码段)

 function contentHeight() { var winH = $(window).outerHeight(true), headerHei = $("header").outerHeight(true), headerPad = $("header").css('padding-top').replace('px',''), footerHei = $("footer").outerHeight(true), footerPad = $("footer").css('padding-top').replace('px',''), paddings = parseInt(footerPad) + parseInt(headerPad), contentHei = winH - (headerHei + footerHei + paddings); $(".content-wrapper").css("min-height", contentHei); } $(document).ready(function () { contentHeight(); }); $(window).resize(function () { contentHeight(); }); 
 *, ::after, ::before { box-sizing: border-box } header{float:left; width:100%; padding;10px 0; background:#ececec;} .content-wrapper{float:left; width:100%; padding:20px; background:#cdcdcd;} footer{float:left; width:100%; padding:15px 0; background:#333333;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <header><p>This is heading</p><p>This is sub-heading</p></header> <div class="content-wrapper"> <p> this is content area </p> <p> this is content area </p> </div> <footer><p>This is footer</p></footer> 

The problem with it is that neither header nor footer has height. 问题是页眉和页脚都没有高度。

CSS CSS

*, ::after, ::before {
box-sizing: border-box
}
header{float:left; position:relative;width:100%;height:30%;top:0;left:0; padding;10px 0; background:#ececec;}
.content-wrapper{float:left; width:100%;padding:20px; background:#cdcdcd;}
footer{float:left; position:relative;width:100%;height:30%;bottom:0;left:0; padding:15px 0;  background:#333333;}

.wrapper{
  width:100vw;
  height:100vh;
  overflow:hidden;
}

JS JS

function contentHeight() {
            var winH = $(window).height(),
                headerHei = $("header").height(),
                footerHei = $("footer").height(),
                contentHei = winH - (headerHei + footerHei);
            $(".content-wrapper").css("height", contentHei - 50 + 'px');
} 
$(document).ready(function () {
            contentHeight();
        });
        $(window).resize(function () {
            contentHeight();
        });

Already tested in the provided fiddler. 已在提供的提琴手中进行过测试。 It worked 有效

Edit:Forgot to mention that I did not used Jquery, I do not know if it is necessary for you to use it. 编辑:忘记提及我没有使用Jquery,不知道您是否有必要使用它。

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

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