简体   繁体   English

滚动后固定导航菜单

[英]Fixed Navigation Menu after Scrolll

I'm trying to create a sticky navigation menu that will be positioned right underneath a banner and when you scroll down and the banner cannot be seen anymore the navigation menu will be fixed at the top of the browser chrome. 我正在尝试创建一个粘贴式导航菜单,该菜单将位于横幅下方,当您向下滚动而无法再看到横幅时,导航菜单将固定在浏览器镶边的顶部。 Here's what I have so far: http://tinyurl.com/bper44a 这是我到目前为止的内容: http : //tinyurl.com/bper44a

The CSS is straight forward, the issue may be with my JS: CSS很简单,问题可能出在我的JS上:

$(document).ready(function() {
    var s = $(".navMenu");
    var pos = s.position();  
    $(window).scroll(function() {
        var windowpos = $(window).scrollTop();
            if (windowpos >= pos.top) {
                s.addClass("fixedTop"); }
            else {
                s.removeClass("fixedTop"); 
                }
        });
    });

While it works exactly the way on want it in Firefox, I can figure out why it behaves differently in Chrome and Safari (gets into fixed position as soon as you scroll down just a little bit). 尽管它完全按照Firefox中的要求运行,但我可以弄清楚为什么它在Chrome和Safari中表现不同(只要向下滚动一点,便会进入固定位置)。

Any insight? 有见识吗?

Not sure why it works in firefox, but I think the following will work for all browsers: 不确定为什么它可以在firefox中使用,但是我认为以下内容适用于所有浏览器:

$(document).ready(function() {
    var s = $(".navMenu");
    var banner = $("header > img");

    $(window).scroll(function() {
        var windowpos = $(window).scrollTop();
            // if the scroll position is greater than the height of the banner
            // fix the navigation. 
            if (windowpos >= banner.outerHeight()) {
                s.addClass("fixedTop"); }
            else {
                    s.removeClass("fixedTop"); 
                }
            });
        });

Obligatory fiddle here . 这里是强制性的小提琴。

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

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