gpt4 book ai didi

javascript - ScrollTop Javascript 在 Firefox 中无法运行

转载 作者:行者123 更新时间:2023-12-03 07:35:13 27 4
gpt4 key购买 nike

我的导航栏无法与 ScrollTop Javascript 一起正常工作。它适用于 Chrome 和 Safari,但不适用于 Firefox。

这是我目前在网站上拥有的所有代码。一旦导航栏位于页面顶部,我希望导航栏跟随滚动。请在 Firefox 中查看,因为这就是我遇到问题的地方!

HTML

<body>

<div class="container">
<nav class="bottom" id="nav">
<div class="buttonWrapper">
<a href="#about">
<div class="navButton">About</div>
</a>
<a href="#designs">
<div class="navButton">Designs</div>
</a>
<a href="#contact">
<div class="navButton">Contact</div>
</a>
</div>
</nav>
<div class="largeLogo"></div>
</div>
<div class="container about" id="about">
<div class="sideBar about">
<div class="sidebarText"></div>
<p></p>
</div>
</div>

<div class="container designs" id="designs">
<div class="view view-ninth">
<img src="images/11.jpg" />
<div class="mask mask-1"></div>
<div class="mask mask-2"></div>
<div class="content">
<h2>Hover Style #9</h2>
<p>Some Text</p>
<a href="#" class="info">Read More</a>
</div>
</div>
<div class="sideBar designs">
<div class="sidebarText"></div>
</div>
</div>

<div class="container contact" id="contact">
<div class="sideBar contact">
<div class="sidebarText"></div>
</div>
</div>

</body>

JavaScript

$(function() {
$('a[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//, '') === this.pathname.replace(/^\//, '') && location.hostname === this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});

window.addEventListener("scroll", navTop, false);

function navTop() {
var nav = document.getElementById("nav");
var about = document.getElementById("about").offsetTop - 1;
if (document.body.scrollTop > about) {
nav.className = "minimize";
} else {
nav.className = "bottom";
}
}

Fiddle

最佳答案

问题出在 document.body.scrollTop 上。尝试使用下面的代码:

function navTop() {
var nav = document.getElementById("nav");
var about = document.getElementById("about").offsetTop - 1;
var scrollTop = $(document).scrollTop();
if (scrollTop > about) {
nav.className = "minimize";
} else {
nav.className = "bottom";
}
}

$(document).scrollTop();/$(window).scrollTop(); 适用于 Firefox 和 Chrome

关于javascript - ScrollTop Javascript 在 Firefox 中无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35643921/

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