如何使菜单栏在滚动时固定在顶部

发布于 2024-12-20 16:48:05 字数 99 浏览 4 评论 0原文

我想制作一个菜单栏,滚动时固定在页面顶部。类似于 Facebook 的顶部菜单。

另外,我想要一个 div 将徽标浮动在菜单栏的左侧,并在菜单栏的右侧放置一个导航浮动。

I'd like to make a menubar, which is fixed on the top of the page while scrolling. Something like the top menu in Facebook.

Also, I want a div holding the logo float at the left of menubar, and a nav float at the right of the menubar.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

戒ㄋ 2024-12-27 16:48:05

这应该可以帮助你开始

 <div class="menuBar">
        <img class="logo" src="logo.jpg"/>
        <div class="nav"> 
            <ul>
                <li>Menu1</li>
                <li>Menu 2</li>
                <li>Menu 3</li>
            </ul> 
        </div>
    </div>



body{
    margin-top:50px;}
.menuBar{
    width:100%;
    height:50px;
    display:block;
    position:absolute;
    top:0;
    left:0;
    }
.logo{
    float:left;
    }
.nav{
    float:right;
    margin-right:10px;}
.nav ul li{
    list-style:none;
    float:left;
    }

This should get you started

 <div class="menuBar">
        <img class="logo" src="logo.jpg"/>
        <div class="nav"> 
            <ul>
                <li>Menu1</li>
                <li>Menu 2</li>
                <li>Menu 3</li>
            </ul> 
        </div>
    </div>



body{
    margin-top:50px;}
.menuBar{
    width:100%;
    height:50px;
    display:block;
    position:absolute;
    top:0;
    left:0;
    }
.logo{
    float:left;
    }
.nav{
    float:right;
    margin-right:10px;}
.nav ul li{
    list-style:none;
    float:left;
    }
高速公鹿 2024-12-27 16:48:05
 #header {
        top:0;
        width:100%;
        position:fixed;
        background-color:#FFF;
    }

    #content {
        position:static;
        margin-top:100px;
    }
 #header {
        top:0;
        width:100%;
        position:fixed;
        background-color:#FFF;
    }

    #content {
        position:static;
        margin-top:100px;
    }
贪了杯 2024-12-27 16:48:05

要将 div 设置在固定位置,您可以使用

position:fixed
top:0;
left:0;
width:100%;
height:50px; /* change me */

to set a div at position fixed you can use

position:fixed
top:0;
left:0;
width:100%;
height:50px; /* change me */
清风疏影 2024-12-27 16:48:05

postition:absolute; 标记相对于其直接父元素定位元素。
我注意到即使在示例中也没有滚动空间,当我尝试时,它不起作用。
因此,要实现 facebook 浮动菜单,应使用 position:fixed; 标签。它将元素替换/保留在给定/指定的位置,并且页面的其余部分可以平滑滚动 - 即使使用响应式页面。

请参阅 CSS 位置属性文档 :)

The postition:absolute; tag positions the element relative to it's immediate parent.
I noticed that even in the examples, there isn't room for scrolling, and when i tried it out, it didn't work.
Therefore, to pull off the facebook floating menu, the position:fixed; tag should be used instead. It displaces/keeps the element at the given/specified location, and the rest of the page can scroll smoothly - even with the responsive ones.

Please see CSS postion attribute documentation when you can :)

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