在我的网站中,我没有设置 html 或 body 标签的高度。这对于内容较多的页面效果很好,但是当页面上的内容不足以填满页面时,我希望正文高度为 100%。
所以我想一点 jQuery 可能会帮助我。我想出了这个小片段,我认为它已经足够了,但不幸的是它并没有做太多事情。
if(jQuery("html").css("height") < "100%"){
jQuery("html, body").css({"height": "100%", "min-height":"100%"});
}
我猜测这可能是因为 jQuery 可能不会以百分比的形式获取主体高度,也许只是以像素为单位。
非常感谢您的帮助。
In my site I have not set a height for a html or a body tag. This works great on pages with lots of content, but when the content on a page is not enough to fill the page up, I want the body height to be 100%.
So I thought a bit of jQuery might help me out. I've come up with this little snippet which I thought would be adequate but unfortunately it doesn't do all too much.
if(jQuery("html").css("height") < "100%"){
jQuery("html, body").css({"height": "100%", "min-height":"100%"});
}
I'm guessing that this may be because the jQuery may not picking up the body height as a percentage, maybe just in pixels.
Would really appreciate your help.
发布评论
评论(2)
您可以使用 100% 高度布局来完成此操作,无需任何 JavaScript。
检查这个:
http://peterned.home.xs4all.nl/examples/csslayout1.html(有在我的项目中使用)
http://www.dave-woods.co.uk/index.php /100-height-layout-using-css/
不管怎样,如果你想通过 jQuery 做到这一点:
你可以定义如果
document
height 小于window
then将文档高度设置为正文或您的主要内容如果你有这样的。
You can do this with 100% height layout. Without any javascript.
Check this:
http://peterned.home.xs4all.nl/examples/csslayout1.html (have used in my projects)
http://www.dave-woods.co.uk/index.php/100-height-layout-using-css/
Anyway, if you want to do this via jQuery:
You can define if
document
height less thenwindow
then set documents height to body or your main<div id=container></div>
if you have such.将以下 css 应用到您的 body/html
html, body { min-height:100%;高度:自动 }
Apply below css to your body/html
html, body { min-height:100%; height:auto }