简体   繁体   English

固定导航栏有两种不同的状态

[英]Fixed Navigation Bar with two different states

What I want to do is have a fixed navigation bar that is 10px from the top of the window unless it is within the first 200px of the document, then I want it to be 200px from the top ... 我想要做的是有一个固定的导航栏,距离窗口顶部10px,除非它在文档的前200px内,然后我希望它从顶部200px ...

So basically I want a navigation bar that is 200px from the top to start off with, but when the user scrolls down 190px the navigation bar scrolls, staying always 10px from the top of the window. 所以基本上我想要一个从顶部开始200px的导航栏,但是当用户向下滚动190px时,导航栏会滚动,从窗口顶部始终保持10px。

You first listen to the scroll event of the window, and then use the scroll value to know what state to apply to you element. 您首先侦听窗口的滚动事件,然后使用滚动值来了解要应用于您的元素的状态。 Example with jQuery : jQuery示例:

var fixed = false, limit = 50;
$(window).scroll(function()
{
    if (window.scrollTop < 50 && fixed)
    {
        $("#header").css({ position: "relative" });
        fixed = false;
    }
    else if (window.scrollTop > 50 && !fixed)
    {
        $("#header").css({ position: "fixed" });
        fixed = true;
    }
});

Also related to this post for code example 还有与此帖子相关的代码示例

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

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