gpt4 book ai didi

javascript - 粘性导航栏在 Firefox 中消失

转载 作者:行者123 更新时间:2023-11-28 10:43:37 25 4
gpt4 key购买 nike

我最近使用以下代码向我网站的导航栏添加了“粘性”功能:

<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 窗口顶部时完全消失了。有什么想法吗?

最佳答案

我的示例是否也发生了同样的情况?如果是这样,我们就能查个水落石出。

代码 + 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');
}
});

});

关于javascript - 粘性导航栏在 Firefox 中消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23065756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com