我正在我的网站上实现下面的代码,在这个论坛的其他地方找到,但只想稍微编辑它,以便如果两张幻灯片是部分在任何时候都可见,则顶部而不是底部被分配为“活动”班级。
$(function() {
var sections = {},
_height = $(window).height(),
i = 0;
// Grab positions of our sections
$('.section').each(function() {
sections[this.name] = $(this).offset().top;
});
$(document).scroll(function() {
var pos = $(this).scrollTop();
// Look in the sections object and see if any section is viewable on the screen.
// If two are viewable, the lower one will be the active one.
for (i in sections) {
if (sections[i] > pos && sections[i] < pos + _height) {
$('a').removeClass('active');
$('#nav_' + i).addClass('active');
}
}
});
});