我正在处理一个带有固定菜单的页面,该菜单在用户从顶部滚动一定距离后拾取,并且当他们向下滚动页面时,菜单中的不同链接被赋予一个改变颜色的类。所有这一切似乎在 Chrome 和 Safari 中运行良好,但在 Firefox 中,页面冻结在顶部。我想知道它是否不断地循环一些代码......基本上冻结了窗口。
这是我的代码。
$.localScroll({
onBefore: function() {
$('body').data('scroll-executing', true);
},
onAfter: function() {
$('body').data('scroll-executing', false);
$(window).trigger("scroll");
}
});
$(window).scroll(function () {
if ($(this).scrollTop() > 259) {
$('.nav').addClass("f-nav");
} else {
$('.nav').removeClass("f-nav");
}
});
$(window).scroll(function() {
if ($('body').data('scroll-executing')) {
return;
}
// find the a with class 'active' and remove it
$("a").removeClass('active');
// get the amount the window has scrolled
var scroll = $(window).scrollTop();
// add the 'active' class to the correct #nav based on the scroll amount
if (scroll > 2150) {
$("#nav_3").removeClass('active');
$("#nav_5").attr('class', 'active');
setHash("contact");
} else if (scroll > 1300) {
$("#nav_2").removeClass('active');
$("#nav_3").attr('class', 'active');
setHash("portfolio");
} else if (scroll > 400) {
$("#nav_2").attr('class', 'active');
setHash("about");
} else if (scroll <= 380) { //when I remove this section, the problem goes away.
$("#nav_1").attr('class', 'active');
setHash("home");
}
});
我忘了添加 setHash 定义。这里是。
setHash = function(hash) {
var scrollmem = $('body').scrollTop();
window.location.hash = hash;
$('html,body').scrollTop(scrollmem);
}
我还注意到 CPU 上升到 100%,我似乎不知道为什么。
问题出在以 else if (scroll <= 380) 开头的代码的第三部分。我通过消除过程弄清楚了这一点。任何人都可以看到它循环或做一些永远不会结束的事情......或者解释为什么 Firefox 在页面顶部冻结?
我对所有这些都是新手......我在过去几天刚刚拿起 jquery,我基本上一直在谷歌搜索并调整代码以使其适合我的需要。
任何帮助将不胜感激。