创建可变高度“固定” CSS 中的标题与可滚动内容
我想在页面上创建一个标题,当您滚动页面时该标题不会移动。
这看起来很简单,只需将“position:fixed”添加到样式中,但内容就会出现在标题下方,因为标题“从流中删除”。所以我认为的解决方案是在内容中添加“margin-top: height”。
当标题的高度可变时,解决此问题的最佳方法是什么?
我有一个小提琴来演示这个问题:
http://jsfiddle.net/waterlooalex/j4Z8F/2 /
如果您的浏览器窗口不是太大,内容文本将滚动到“hello world header”下方,问题是第一行文本“Lorem ipsum...”被隐藏。我已经得到了一些解决这个问题的注释。
I'd like to create a header on the page that doesn't move when you scroll the page.
This seems simple, just add "position: fixed" to the style, but then the content appears underneath the header, because the header is "removed from the flow". So the solution I think is to add "margin-top: height" to the content.
Whats the best way to solve this when the height of the header is variable?
I've got a fiddle that demonstrates the problem:
http://jsfiddle.net/waterlooalex/j4Z8F/2/
Providing your browser window is not too large, the content text will scroll below the 'hello world header', the problem is that the first line of text "Lorem ipsum...' is hidden. I've got some comment out javascript that works around the issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以添加未固定的标题 div 的第二个副本,并在其上使用类似
visibility:hidden
的内容。这样,它就可以将内容推送到您需要的确切位置,但无需使用 JavaScript。有点黑客,但我在你的小提琴中尝试过,它有效。You could add a second copy of the header div that's not fixed and use something like
visibility:hidden
on it. That way it pushes the content down to the exact spot you need, but without using JavaScript. Kinda hackish but I tried it in your fiddle and it works.您可以使用 flexbox 布局模型来解决此问题,如下所示下面的演示。
请注意,出于演示目的,我已将
display:flex
添加到中,理想情况下您应该将其赋予更具体的父元素。
You can solve this using flexbox layout model as shown in the below demo.
Note that I've added
display:flex
to<body>
for demo purpose, ideally you should give it to a more specific parent element.此 JavaScript 将采用固定标题的可变高度,并将内容的上边距设置为在下方流动。只需调用onload即可
This javascript will take the variable height of a fixed header and set the top margin of the content to flow underneath. Just call onload
为了解决这个问题,我最终在我的内容中添加了一个“虚拟”div。
该 div 与标题 div 具有相同的高度,因此将内容向下推得足以创建正确的偏移量。
这与@curtisdf 上面的答案相同,但是他建议复制您的标题,我建议制作一个具有相同高度的空 div。
由于高度可以改变的原因有很多(窗口大小调整、内容更改等),我最终使用 https://github.com/marcj/css-element-queries/blob/master/src/ResizeSensor.js 保持虚拟 div 的高度与这真实标头:
请注意,我可以引用真实标头 (me.summary) 和虚拟标头 (me.summaryDummy),因为 aurelia.io 的 ref 实现,但您可以使用 JQuery 或通过 ID 获取对 div 的引用。
To fix this, I ended up adding a 'dummy' div in my content.
This div has the same height as the header div, thus pushes the content down exactly enough to create the correct offset.
This is the same answer as the one above by @curtisdf, however he suggests making a copy of your header, I'm suggesting making an empty div with the same height.
Since there's many reasons the height can change (window resize, content change, ...) I ended up using https://github.com/marcj/css-element-queries/blob/master/src/ResizeSensor.js to keep the height from the dummy div in sync with the real header:
Note that I can reference the real header (me.summary) and the dummy (me.summaryDummy) because of aurelia.io's ref implementation, but you get a reference to your divs using JQuery or via the ID.
无论标题有多高,我总是在内容顶部设置边距,因为我的标题通常不需要更改高度。
如果你的标题确实改变了高度,那么我担心你将不得不使用 JavaScript 来解决它。脱离流程意味着页面的其余部分将正常进行,并且 JavaScript 必须介入以提供帮助。
I have always set a margin on top of the content of however high the header is as my header's normally never need to change height.
If your header does change height then your going to have to go for the JavaScript to get around it, I'm afraid. Being out of the flow means the rest of the page will carry on as normal and JavaScript will have to step in to help.