Skip to content

Instantly share code, notes, and snippets.

@victorstanciu
Created October 19, 2012 14:03
Show Gist options
  • Save victorstanciu/3918384 to your computer and use it in GitHub Desktop.
Save victorstanciu/3918384 to your computer and use it in GitHub Desktop.
document.on('dom:loaded', function () {
var spies = $$('a[data-spy]');
var targets = [];
for (var i = 0; i < spies.length; i++) {
var target = $(spies[i].getAttribute('data-spy'));
target['data-spy'] = spies[i];
targets.push(target);
}
var active_target = null;
function activate(target) {
var spy = target['data-spy'];
spy.up('ul').select('li').invoke('removeClassName', 'active');
spy.up('li').addClassName('active');
}
var global_offset = 50;
window.onscroll = function () {
var scroll = document.documentElement.scrollTop || document.body.scrollTop;
scroll += global_offset;
for (var i = 0; i < targets.length; i++) {
var target = targets[i];
var offset = target.cumulativeOffset().top;
var offset_next = targets[i + 1] ? targets[i + 1].cumulativeOffset().top : false;
if (active_target != target && scroll > offset && (!offset_next || scroll < offset_next)) {
activate(target);
}
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment