将选项卡固定在页面顶部,但位于标题下方

发布于 2024-08-23 06:06:20 字数 255 浏览 3 评论 0原文

我正在尝试弄清楚如何为网站设计标题,以便在标题下方放置一些选项卡(看起来像是从标题下方伸出),当页面向下滚动时,这些选项卡与标题保持在一起(并且标题向上移动),但是当它们到达页面顶部时会固定在那里,以便它们始终可见。

我知道如何将元素固定到页面顶部,以便其始终可见,但是当标题位于窗口中时,我希望选项卡显示在其下方,而不是顶部。当标题离开页面时,选项卡仍然存在。

我需要的是使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

微暖i 2024-08-30 06:06:20

请参阅此SO 问题和答案

See this SO question, and answers

雪花飘飘的天空 2024-08-30 06:06:20

基本方法是在页面加载时绝对定位元素,但在滚动时,将定位更改为固定(如果支持),或者将元素移动到适当的位置。

<div id="fix" style="position:absolute;top:30px;">
My fixedish div
</div>

还有一些快速的 js(应该清理以跨浏览器工作):

// register event handler
window.addEventListner("scroll", function(){
    var div = document.getElementById("fix");  
    if (document.body.scrollHeight > 30) {
        // fix it to the top
        div.style.position = "fixed";
        div.style.top = "0px";
    }
    else {
        // position it below the header
        div.style.position = "absolute";
        div.style.top = "30px";
    }
});

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.

<div id="fix" style="position:absolute;top:30px;">
My fixedish div
</div>

And some quick js (should be cleaned up to work cross-browser):

// register event handler
window.addEventListner("scroll", function(){
    var div = document.getElementById("fix");  
    if (document.body.scrollHeight > 30) {
        // fix it to the top
        div.style.position = "fixed";
        div.style.top = "0px";
    }
    else {
        // position it below the header
        div.style.position = "absolute";
        div.style.top = "30px";
    }
});
梅窗月明清似水 2024-08-30 06:06:20

Remy Sharp,来自 Left LogicjQuery 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.

温折酒 2024-08-30 06:06:20

您可以查看 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.

无尽的现实 2024-08-30 06:06:20

我不认为你可以只使用 css 来做到这一点,正如 SLaks 所说。即使有某种我不知道的条件语句或黑客方法,我也不会这样做,主要有两个原因:

  • 它肯定不适用于所有浏览器。
  • 这当然不是合适的选择(语言应该与问题相匹配)。

使用 javascript,我建议使用 onScrollscrollHeight 来更新位于页面顶部的选项卡 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:

  • It would certainly not work with all browsers.
  • It certainly is not the appropiate choice (the language should match the problem).

Using javascript, I would suggest to use onScroll and scrollHeight to update the tabs css when they are at the top of the page.

一指流沙 2024-08-30 06:06:20

直到我添加了这个“z-index: 999;”之后,我的才在下面工作。

Mine wasn't work underneath until I added this "z-index: 999;"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文