将选项卡固定在页面顶部,但位于标题下方
我正在尝试弄清楚如何为网站设计标题,以便在标题下方放置一些选项卡(看起来像是从标题下方伸出),当页面向下滚动时,这些选项卡与标题保持在一起(并且标题向上移动),但是当它们到达页面顶部时会固定在那里,以便它们始终可见。
我知道如何将元素固定到页面顶部,以便其始终可见,但是当标题位于窗口中时,我希望选项卡显示在其下方,而不是顶部。当标题离开页面时,选项卡仍然存在。
我需要的是使用 CSS 和/或 JavaScript 的解决方案。有谁知道有什么方法可以做到这一点?
I'm trying to figure out how to design a header for a website so that there are tabs placed underneath the header (that look like they're sticking out from under the header), that stay with the header as the page is scrolled down (and the header moves up), but when they reach the top of the page become fixed there so that they're always visible.
I know how to fix an element to the top of the page so that its always visible, but when the header is in the window, I want the tabs to appear below it, not at the top. When the header is off the page, the tabs will still be there.
What I need is a solution using CSS and/or JavaScript. Does anyone know of a way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
请参阅此SO 问题和答案
See this SO question, and answers
基本方法是在页面加载时绝对定位元素,但在滚动时,将定位更改为固定(如果支持),或者将元素移动到适当的位置。
还有一些快速的 js(应该清理以跨浏览器工作):
The basic approach is to position the element absolutely on page load but on scroll, change the positioning to
fixed
if supported, or move the element to the appropriate location.And some quick js (should be cleaned up to work cross-browser):
Remy Sharp,来自 Left Logic 和 jQuery for Designers 提供了有关类似主题的教程:-ish a href="http://jqueryfordesigners.com/fixed-floating-elements/" rel="nofollow noreferrer" title="最后是教程本身的链接。">fixed-floating-elements 。
这模拟了 英国 Apple 商店(单击要配置的产品,并在滚动页面时观看摘要/规格框)几乎涵盖了您需要了解的所有内容,以适应技术以满足您自己的要求。
显然,从 URL 到站点,它确实需要(或者至少使用)jQuery,而不是纯 JavaScript。
Remy Sharp, of Left Logic, and jQuery for Designers offers a tutorial on a similar-ish subject: fixed-floating-elements.
This emulates the shopping basket from the UK Apple store (click on a product to configure, and watch the summary/specification boxes as you scroll the page) covers pretty much everything you should need to know to adapt the technique to suit your own requirements.
Obviously, from the URL to the site, it does require (or, at least, makes use of) jQuery, rather than plain Javascript.
您可以查看 jQuery UI,它有一个选项卡插件,在简短搜索后您可以找到许多使用示例。
You could look into jQuery UI which has a tabs plugin, and there are many examples for use of that you can find after s short search.
我不认为你可以只使用 css 来做到这一点,正如 SLaks 所说。即使有某种我不知道的条件语句或黑客方法,我也不会这样做,主要有两个原因:
使用 javascript,我建议使用
onScroll
和scrollHeight
来更新位于页面顶部的选项卡 css。I don't think you can do that with css only, as SLaks said. Even if there is some way of conditional statements or hacks I don't know of, I wouldn't do it because of mainly two reasons:
Using javascript, I would suggest to use
onScroll
andscrollHeight
to update the tabs css when they are at the top of the page.直到我添加了这个“z-index: 999;”之后,我的才在下面工作。
Mine wasn't work underneath until I added this "z-index: 999;"