Skip to content

Instantly share code, notes, and snippets.

@kopiro
Last active December 28, 2015 20:49
Show Gist options
  • Select an option

  • Save kopiro/7559969 to your computer and use it in GitHub Desktop.

Select an option

Save kopiro/7559969 to your computer and use it in GitHub Desktop.
Scroller Spyer
var Scroller = {
registeredHandlers: [],
install: function(opt) {
Scroller.registeredHandlers.push(opt);
},
onScroll: function(){
var now = window.scrollY;
$.each(Scroller.registeredHandlers, function(){
if (this.after) {
if (now>=this.after) {
if (!this.permanentAfter || !this.afterYet){
this.afterYet = true;
if (this.cb) this.cb(true);
if (this.el) this.el.addClass(this.customClass?this.customClass:'on');
}
} else {
if (!this.permanentAfter) {
if (this.cb) this.cb(false);
if (this.el) this.el.removeClass(this.customClass?this.customClass:'on');
}
}
}
if (this.between) {
if (now>=this.between[0] && now<=this.between[1]) {
if (this.cb) this.cb(true);
if (this.el && !this.el.hasClass(this.customClass?this.customClass:'on')) {
this.el.addClass(this.customClass?this.customClass:'on');
}
} else {
if (this.cb) this.cb(false);
if (this.el && this.el.hasClass(this.customClass?this.customClass:'on')) {
this.el.removeClass(this.customClass?this.customClass:'on');
}
}
}
if (this.event) {
this.event();
}
});
},
init: function(){
$(window).scroll(Scroller.onScroll);
Scroller.onScroll();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment