我很可能会遗漏一些东西,或者完全做错了,但是这一点javascript在Chrome和Safari中运行良好,而不是Firefox。 section.id(在if语句中)似乎没有改变,但总是返回section [0] .id,无论它是否符合if语句标准。
var localNavigation = {
showTab: function (anchortags, anchors) {
$(window).scroll(function () {
var scroller, i, section, sectionTop, sectionBottom, sectionColor, sectionTextColor, $section;
for (i = 0; i < anchors.length; i++) {
scroller = document.body.scrollTop;
section = anchors[i];
$section = $(section);
sectionColor = $section.css("background-color");
sectionTextColor = $section.css("color");
sectionTop = section.offsetTop;
sectionBottom = section.offsetTop + section.offsetHeight;
if ((scroller > sectionTop) && (scroller < sectionBottom)) {
$(anchortags).removeClass("selected").css({
"background-color": "transparent",
"color": "#f5eeed"
});
$("#" + section.id + "_tab").addClass("selected").css({
"background-color": "" + sectionColor + "",
"color": "" + sectionTextColor + ""
});
console.log(section.id);
}
}
});
},
init: function () {
var anchortags = $("#local ul li a"),
anchors = $("section");
localNavigation.showTab(anchortags, anchors);
}
};
localNavigation.init();
答案 0 :(得分:0)
仔细检查您是否正在使用正确的文件。
还尝试使用每个循环。
anchors.each(function( i, section ){
var $section = $(section);
scroller = document.body.scrollTop;
sectionColor = $section.css("background-color");
sectionTextColor = $section.css("color");
sectionTop = $section.offsetTop;
sectionBottom = $section.offsetTop + $section.offsetHeight;
if ((scroller > sectionTop) && (scroller < sectionBottom)) {
console.log($section.id);
}
});
答案 1 :(得分:0)
我发现您使用的是HTML 5 <section>
标记。之前在StackOverflow上已经讨论过许多版本的Firefox还不支持这个标签,所以你是否尝试过确保使用最新版本的Firefox?
如果失败了,您可以尝试使用其他标记的代码吗?