简体   繁体   English

固定的页眉/页脚,可滚动的内容,上方/下方的空间

[英]Fixed Header/Footer, Scrollable Content, Space Above/Below

I've seen several similar questions to this (and done plenty more research) but I don't think any of them provide a solution to this specific problem... 我已经看到了几个与此类似的问题(并进行了大量研究),但我认为其中没有一个为这个特定问题提供解决方案...

I made a quick example of what I mean here: https://gist.github.com/anonymous/62bb6b6e405549a36ae9 我做了一个简短的例子来说明我的意思: https : //gist.github.com/anonymous/62bb6b6e405549a36ae9

That's not the actual code I'm using (since I am working with WordPress) but it's illustrative of my issue, which is, specifically: 那不是我正在使用的实际代码(因为我正在使用WordPress),但是它说明了我的问题,特别是:

I am working on a site which will have a fixed Header and Footer, with content in between. 我正在一个站点上有一个固定的页眉和页脚,中间是内容。 I've already done so, it works fine. 我已经这样做了,它工作正常。 However, I do NOT want the Header and Footer to be touching the top and bottom of the browser (they'll have, for example, a 20px margin on them, so there won't be a lot of space between the containers and the browser). 但是,我不希望页眉和页脚触及浏览器的顶部和底部(例如,它们在其上具有20px的边距,因此在容器和容器之间没有太多的空间)浏览器)。

The problem is that when you scroll the page, the content is visible in the space between the header/browser, and the footer/browser. 问题是,当您滚动页面时,该内容在页眉/浏览器与页脚/浏览器之间的空间中可见。 As in, you can see it scrolling above the header and below the footer; 如图所示,您可以看到它在页眉上方和页脚下方滚动; I'd like to avoid this. 我想避免这种情况。

Is there some way to style the content container in a way that won't make it show above the header/below the footer? 有什么方法可以使内容容器样式化,而不会使其显示在页眉上方/页脚下方?

At the moment I'm using a background image on the Header and Footer, and pushed them up against the edge of the browser; 目前,我在页眉和页脚上使用了背景图片,并将它们推向浏览器的边缘。 but it's very design-restrictive and I don't want to lock myself into doing it that way. 但这是设计上的限制,我不想让自己那样做。

NOTE: My specs for this project are that the background must be very image-heavy, which is why I'm particular about the header and footer. 注意:我对这个项目的规格是背景必须非常重图像,这就是为什么我特别关注页眉和页脚。 It's really restricting the images that we can use because we have to assume that the detailed background is getting cut off, and we can't control how it cuts -- meaning I can't just cut the header and footer into images to "fake" this look. 这实际上限制了我们可以使用的图像,因为我们必须假定详细背景已经被切除,并且我们无法控制其剪切方式-这意味着我不能仅将页眉和页脚剪切成图像以进行“伪造”这个样子。

Instead of using a margin, can you just use a div container that is fixed position. 您可以只使用固定位置的div容器,而不必使用边距。

#whitebox {position:fixed; margin-top:-20px; background-color:blue;width:100%;height:24px;}

See example based off your code . 请参见基于您的代码的示例 Just make the color white or whatever is your background color. 只需将颜色设为白色或您的背景颜色即可。

If you are allowed to use jquery. 如果允许使用jquery。 you can calculate the distance between the fixed boxes and place your content there. 您可以计算固定框之间的距离,然后将内容放置在那里。

I added an inner div to fill the fixed content div. 我添加了一个内部div来填充固定内容的div。

Slight css changes. 轻微的CSS变更。

Fiddle here 在这里摆弄

var topOffset = $('#header').offset().top + 30;
var bottomOffset = $('#footer').offset().top;
var distance      = (bottomOffset - topOffset);
$("#content").css("height", distance);

UPDATE You might also consider recalculating when the browser is resized Turn above code into a function and then call from resize as well 更新您可能还考虑在调整浏览器大小时重新计算将上述代码转换为函数,然后也从调整大小中调用

JQuery Resize jQuery调整大小

$( window ).resize(function() {
  $( "#log" ).append( "<div>Handler for .resize() called.</div>" );
});

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

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