Skip to content

Instantly share code, notes, and snippets.

@DimitarChristoff
Created February 5, 2012 19:19
Show Gist options
  • Save DimitarChristoff/1747519 to your computer and use it in GitHub Desktop.
Save DimitarChristoff/1747519 to your computer and use it in GitHub Desktop.
initial draft of a scroll spy like in twitter by arian. to be made more like js garden one.
var BootstrapScrollSpy = new Class({
initialize: function(element, wrapper){
element = this.element = document.id(element);
this.wrapper = wrapper || window;
var links = this.links = element.getElements('a');
var elements = this.elements = [];
links.each(function(el){
var href = el.get('href'), target;
if (href.slice(0, 1) == '#') target = document.id(el.get('href').slice(1));
elements.push(target);
});
this.attach();
},
attach: function(){
if (!this.boundScroll) this.boundScroll = this.scroll.bind(this);
this.wrapper.addEvent('scroll', this.boundScroll);
},
detach: function(){
if (this.boundScroll) this.wrapper.removeEvent('scroll', this.boundScroll);
},
scroll: function(){
var top = this.wrapper.getScroll().y;
var last, index;
this.elements.some(function(el, i){
if (!el) return false; // skip empty entry
var y = el.getPosition(this.wrapper == window ? document.body : this.wrapper).y;
if (y < top) index = i;
return y >= top;
}, this);
if (index != this.active){
if (this.active != null) this.links[this.active].removeClass('active');
this.active = index;
if (index != null) this.links[index].addClass('active');
}
}
});
// var scroll = new BootstrapScrollSpy('navbar');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment