I think I understand what you are looking to do, I had the same problem just recently.
You need to compare the $(document).scrollTop()
with the $(section).offset().top
of each sections. Once you got the ID of the active section find the <a>
with the same href to apply some highlighting.
$(document).scroll(function(){
var st = $(this).scrollTop();
$(section).each(function() {
if(st > $(this).offset().top && st <= $(this).offset().top + $(this).height() ){
var id = $(this).attr('id');
$('a[href="#'+id+'"]').addClass('active');
}else{
var id = $(this).attr('id');
$('a[href="#'+id+'"]').removeClass('active');
}
});
});
You can check out my fiddle
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…