简体   繁体   English

标题/内容/页脚布局,可滚动内容和页脚固定到视口底部

[英]Header/Content/Footer layout with scrollable content and footer pinned to viewport bottom

There are lots of quasi-duplicates to this question, I know: webpage template where content takes full height of viewport if has 1 line minus footer is one, but that's not what I want; 这个问题有很多准重复,我知道: 网页模板,如果1行减去页脚是1, 则内容占视口的全高 ,但这不是我想要的; there's this hilarious question: How to create an HTML CSS Page with Header, Footer and Content that mostly describes what I want, I think, but unfortunately it's somewhat incoherent and there's not really an answer. 这是一个热闹的问题: 如何创建一个带有页眉,页脚和内容的HTML CSS页面 ,主要描述我想要的东西,我想,但不幸的是它有点不连贯,并没有真正的答案。 I've found lots of help doing things I could probably figure out for myself, and which I don't want: 我找到了许多帮助做我可能想到的事情,我不想要的事情:

  • Fixed header, footer fixed at bottom for short content but pushed down off page when content is longer (like this oft-repeated setup here: http://boagworld.com/technology/fixed-footers-without-javascript ) 固定页眉,页脚固定在底部以获取短内容,但在内容较长时将页面向下推(如此重复设置: http//boagworld.com/technology/fixed-footers-without-javascript
  • Fixed header, content, and footer, where footer isn't pinned to the viewport bottom 修复了页眉,内容和页脚,其中页脚未固定到视口底部
  • Fixed header and footer where content scrolls "behind" the header and footer - this one is cute and I've used it but it's not what I want at the moment 固定页眉和页脚,内容在页眉和页脚“后面”滚动 - 这个很可爱,我已经使用过了,但这不是我现在想要的

I know how I'd get what I want if I were using the "broken" or "border-box" box model: 我知道如果我使用“破损”或“边框”框模型,我会得到我想要的东西:

  1. Put a 100% height container wherever I wanted it horizontally on the page, with "position: relative" to make dealing with the contents a little easier 在页面上水平放置100%高度的容器,使用“position:relative”处理内容更容易一些
  2. In the container, put three absolutely-positioned divs: the header, which gets stuck to the top (with a fixed height); 在容器中,放置三个绝对定位的div:头部,卡在顶部(固定高度); the footer, stuck to the bottom (also fixed height); 页脚,粘在底部(也固定高度); and the content, with height 100% but with padding at top and bottom to account for the header and footer. 和内容,高度为100% 顶部和底部有填充,以说明页眉和页脚。

In the "broken" box model, giving the content box 100% height worked great, because the height included the top and bottom padding. 在“破碎”框模型中,给予内容框100%高度的效果很好,因为高度包括顶部和底部填充。 This even worked great in IE6 quirks mode, and for Firefox I'd have just used "-moz-box-sizing: border-box;" 这甚至在IE6怪癖模式下运行良好,对于Firefox我刚刚使用了“-moz-box-sizing:border-box;” to make it work the same way. 让它以同样的方式工作。

But here we are, living in the future, with moon colonies and frozen breakfast pizzas, so I try not to reminisce much about the "border-box" days. 但在这里,我们生活在未来,与月亮殖民地和冷冻早餐比萨饼,所以我尽量不回忆太多关于“边境盒”的日子。 The hardest thing for me to "get" with this layout technique is how to do the scrolling content. 对于我来说,“获得”这种布局技术最困难的是如何进行滚动内容。 The only approach that I can think of is a hackish variation on the above: 我能想到的唯一方法就是上面的一个hackish变种:

  1. Again, start with a 100% height container, "position: relative" 再次,从100%高度的容器开始,“位置:相对”
  2. Again, absolutely-positioned header and footer, with fixed heights. 同样,绝对定位的页眉和页脚,具有固定的高度。
  3. For the content, I'd absolutely drop in a div with no specific height, but with "top" and "bottom" set according to the header and footer heights. 对于内容,我绝对放入没有特定高度的div,但根据页眉和页脚高度设置“顶部”和“底部”。

That technique won't really work in IE6; 这种技术在IE6中无法真正起作用; well, in fact it won't work at all, because IE6 does not derive height from the implication of setting both "top" and "bottom". 好吧,事实上它根本不起作用,因为IE6并没有从设置“顶部”和“底部”的含义中获得高度。 I could use that "active" stuff in the CSS that IE supports to effectively compute the height with Javascript, but I consider that to be pretty disgusting. 我可以在IE支持的CSS中使用那些“活跃”的东西,用Javascript有效地计算高度,但我认为这非常恶心。

I've been through a lot of descriptions of very similar layout problems on the web, but the key thing that I have yet to find is a good technique for getting that middle content box to scroll. 我在网上经历过很多关于非常相似的布局问题的描述,但是我还没有找到关键的东西是让中间内容框滚动的好方法。 I have a feeling that it might be possible to use a content box with top- and bottom-margin set to account for the header and footer, but I don't know how to constrain its height so that the scroll bar would kick in at the right point (when the actual contents overflow the space between the bottom of the header and the top of the footer). 我有一种感觉,可能会使用带有上边距和下边距设置的内容框来说明页眉和页脚,但我不知道如何约束它的高度,以便滚动条可以启动正确的点(当实际内容溢出页眉底部和页脚顶部之间的空间时)。

Ideas? 想法? Links to treasure-troves of templates? 链接到模板的宝库? (Btw Matthew James Taylor's domain seems to be gone at the moment, creating a great disturbance in the Force.) I'm pretty much out of ideas. (顺便说一下,马修詹姆斯泰勒的领域似乎已经消失了,给部队带来了很大的干扰。)我几乎没有想法。

update Here is a sample of what I want: http://gutfullofbeer.net/dreamlayout.html 更新以下是我想要的示例: http//gutfullofbeer.net/dreamlayout.html

That example page should work in Firefox and Chrome and I think Safari, and it half-works in IE8 (only half because of course we couldn't have expected Microsoft to get "-ms-border-box" to work properly). 该示例页面应该可以在Firefox和Chrome中运行,我认为Safari,它在IE8中运行一半(只有一半因为我们当然不能指望微软让“-ms-border-box”正常工作)。

Also I'm adding the "javascript" tag in case some genius can give me an IE hack that's not too revolting. 此外,我正在添加“javascript”标签,以防一些天才可以给我一个不太反感的IE黑客。

another update Here's the "compromise" layout, where the central content scrolls "under" the header and footer, with the scroll bar being supplied by an outer container. 另一个更新这里是“妥协”布局,其中中心内容在页眉和页脚的“下方”滚动,滚动条由外部容器提供。 It actually looks kind-of cute, depending on your tastes, and it works in IE6 and I think everywhere else (though I haven't tried Opera). 它实际上看起来很可爱,取决于你的口味,它在IE6中工作,我想在其他地方(虽然我没有尝试过Opera)。 Ignore the colors of course; 当然要忽略颜色; they're just there as diagnostic aids. 他们只是作为诊断辅助工具。

http://gutfullofbeer.net/compromiselayout.html http://gutfullofbeer.net/compromiselayout.html

Maybe this will help you: (you need jQuery) 也许这会对你有所帮助:(你需要jQuery)

 $(function () { $("#toggle-content").click(function() { $(".more-content").toggleClass("less-content"); }); }); 
 body { height: 100%; margin: 0 auto; width: 50%; } .fixed-top { width: 100%; height: 20vh; background: #faa; } .wrapper { positon: static; height: 100%; width: 100%; } .content { position: relative; width: 100%; height: 20vh; background: #ffa; } .fixed-bot { position: fixed; width: 50%; height: 20vh; background: #faa; opacity: .5; bottom: 0; } .more-content { /* display: block; */ overflow-y: scroll; transition: all 0.3s ease; height: 40vh; } .less-content { height: 0; } /* other styles */ h1,h2 { margin-bottom: 0; margin-top: 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <header class="fixed-top"><h1>THIS IS HEADER CONTENT</h1> <button id="toggle-content" type="button">MORE/LESS</button> </header> <section class="wrapper"> <section class="content"><h2>CONTENT</h2>, Donec condimentum neque vel purus sodales, pulvinar blandit ante pulvinar. Pellentesque tempor, mi non iaculis volutpat, nibh nulla laoreet nisi, viverra laoreet diam nunc vitae dui. </section> <section class="more-content"><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum iaculis, dolor quis pharetra bibendum, purus est porta purus, eu elementum orci tortor eu metus. Pellentesque et neque id metus tincidunt maximus. Mauris ac enim iaculis, interdum tellus a, congue arcu. Proin id justo ut nisi pharetra suscipit fermentum et tortor. Nullam felis libero, sodales a lacus vel, molestie feugiat odio. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Phasellus lorem diam, tincidunt sit amet ex quis, ullamcorper euismod urna. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Quisque in tristique lorem, quis placerat diam. Nullam eleifend odio at dui cursus faucibus. Quisque ac nisi porttitor, molestie est non, facilisis dui. Donec condimentum neque vel purus sodales, pulvinar blandit ante pulvinar. Pellentesque tempor, mi non iaculis volutpat, nibh nulla laoreet nisi, viverra laoreet diam nunc vitae dui. Suspendisse a dignissim dolor, quis efficitur libero. Fusce enim sem, consequat nec rhoncus eu, euismod ac velit.</p><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum iaculis, dolor quis pharetra bibendum, purus est porta purus, eu elementum orci tortor eu metus. Pellentesque et neque id metus tincidunt maximus. Mauris ac enim iaculis, interdum tellus a, congue arcu. Proin id justo ut nisi pharetra suscipit fermentum et tortor. Nullam felis libero, sodales a lacus vel, molestie feugiat odio. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Phasellus lorem diam, tincidunt sit amet ex quis, ullamcorper euismod urna. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Quisque in tristique lorem, quis placerat diam. Nullam eleifend odio at dui cursus faucibus. Quisque ac nisi porttitor, molestie est non, facilisis dui. Donec condimentum neque vel purus sodales, pulvinar blandit ante pulvinar. Pellentesque tempor, mi non iaculis volutpat, nibh nulla laoreet nisi, viverra laoreet diam nunc vitae dui. Suspendisse a dignissim dolor, quis efficitur libero. Fusce enim sem, consequat nec rhoncus eu, euismod ac velit.</p><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum iaculis, dolor quis pharetra bibendum, purus est porta purus, eu elementum orci tortor eu metus. Pellentesque et neque id metus tincidunt maximus. Mauris ac enim iaculis, interdum tellus a, congue arcu. Proin id justo ut nisi pharetra suscipit fermentum et tortor. Nullam felis libero, sodales a lacus vel, molestie feugiat odio. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Phasellus lorem diam, tincidunt sit amet ex quis, ullamcorper euismod urna. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Quisque in tristique lorem, quis placerat diam. Nullam eleifend odio at dui cursus faucibus. Quisque ac nisi porttitor, molestie est non, facilisis dui. Donec condimentum neque vel purus sodales, pulvinar blandit ante pulvinar. Pellentesque tempor, mi non iaculis volutpat, nibh nulla laoreet nisi, viverra laoreet diam nunc vitae dui. Suspendisse a dignissim dolor, quis efficitur libero. Fusce enim sem, consequat nec rhoncus eu, euismod ac velit.</p> </section> <footer class="fixed-bot"> </footer> </section> 

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

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