Sticky导航栏在Firefox中消失

时间:2014-04-14 16:50:20

标签: javascript jquery html css nav

我最近添加了"粘贴"使用以下代码访问我网站的导航栏:

<nav>
    <div class="menu-navigation-container">
        <ul class="menu" id="menu-navigation">
            <li class="menu-item" id="menu-item-50"><a href="#">BlahBlah</a></li>
            <li class="menu-item" id="menu-item-51"><a href="#">BlahBlah</a></li>
            <li class="menu-item" id="menu-item-49"><a href="#">BlahBlah</a></li>
            <li class="menu-item" id="menu-item-48"><a href="#">BlahBlah</a></li>
            <li class="menu-item" id="menu-item-47"><a href="#">BlahBlah</a></li>
        </ul>
    </div>            
</nav>

负责棒的JavaScript:

$(document).ready(function () {
    var header = $('nav').position().top;
    var navHeight = $('nav').outerHeight();

    $(window).scroll(function () {
        var windowpos = $(window).scrollTop();

        if(windowpos > header) {
                $('nav').addClass('fixed').css('top', '0').next().css('margin-top', navHeight + 'px');
            }
            else {
                $('nav').removeClass('fixed').next().css('margin-top', '0');
            }
    });
});

.sticky CSS类:

.fixed {
    -webkit-box-shadow: 0 4px 10px -3px #000;
    -moz-box-shadow: 0 4px 10px -3px #000;
    -o-box-shadow: 0 4px 10px -3px #000;
    box-shadow: 0 4px 10px -3px #000;
    position: fixed !important;
}

它在Chrome,Safari和IE上运行得非常好,但出于某种原因,导航栏在Firefox中到达窗口顶部时完全消失。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我的例子是否也发生了同样的事情?如果是这样,我们就可以了解它。

Code + JSFiddle

http://jsfiddle.net/fbfWG/

HTML

<nav>
    <div class="menu-navigation-container">
        <ul class="menu" id="menu-navigation">
            <li class="menu-item" id="menu-item-50"><a href="#">BlahBlah</a>

            </li>
            <li class="menu-item" id="menu-item-51"><a href="#">BlahBlah</a>

            </li>
            <li class="menu-item" id="menu-item-49"><a href="#">BlahBlah</a>

            </li>
            <li class="menu-item" id="menu-item-48"><a href="#">BlahBlah</a>

            </li>
            <li class="menu-item" id="menu-item-47"><a href="#">BlahBlah</a>

            </li>
        </ul>
    </div>
</nav>

CSS

body {
    height:2000px;
    margin:100px 0 0 0;
    padding:0;
}
nav {
    padding:10px;
    background:red;
    width:100%;
}
a {
    text-decoration:none;
}
ul {
    margin:0;
    padding:0;
    list-style-type:none;
}
ul li {
    display:inline-block;
    margin-right:10px;
}
.fixed {
    position: fixed;
    top:0;
    left:0;
    width:100%;
}
/* apply a natural box layout model to all elements */
 *, *:before, *:after {
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}

JS

$(document).ready(function () {
    var header = $('nav').offset().top;
    var navHeight = $('nav').outerHeight();

    $(window).scroll(function () {
        var windowpos = $(window).scrollTop();

        if (windowpos > header) {
            $('nav').addClass('fixed').next().css('margin-top', navHeight + 'px');
        } else {
            $('nav').removeClass('fixed').next().css('margin-top', '0');
        }
    });

});