我正在我的网站上实现下面的code,在本论坛的其他地方找到,但是只想略微编辑它,以便如果两个幻灯片的部分在任何时候都可以看到顶部而不是bottom被赋予'active'类。
$(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');
}
}
});
});