tinyEmitter
function tinyEmitter() {
}
tinyEmitter.prototype.on = function(evtype, fn) {
this._events = this._events || {};
this._events[evtype] = this._events[evtype] || [];
if (this._events[evtype].indexOf(fn) !== -1) return;
this._events[evtype].push(fn);
}
tinyEmitter.prototype.off = function(evtype, fn) {
this._events = this._events || {};
if (evtype in this._events === false) return;
fn && this._events[evtype].splice(this._events[evtype].indexOf(fn), 1);
!fn && delete this._events[evtype];
}
tinyEmitter.prototype.emit = function(evtype) {
this._events = this._events || {};
var self = this, args = [].slice.call(arguments, 1);
if (evtype in this._events === false) return;
this._events[evtype].forEach(function(fn, i) {
fn.apply(self, args);
});
}
tinyEmitter.eventify = function(Klass) {
Klass.prototype = Object.create(Klass.prototype);
for (var attr in tinyEmitter.prototype) {
Klass.prototype[attr] = tinyEmitter.prototype[attr]
}
return Klass;
}
tinyScroll
function tinyScroll() {
}
tinyEmitter.eventify(tinyScroll);
tinyScroll.prototype.init = function() {
var self = this;
self.checkDir();
}
tinyScroll.prototype.checkDir = function(e) {
var self = this, scrollTop = self.scrollTop = self.scrollTop || document.body.scrollTop;
if (scrollTop > document.body.scrollTop) {
self.emit('up', e);
}
if (scrollTop < document.body.scrollTop) {
self.emit('down', e);
}
self.emit('scroll', e);
}
// use
var s = new tinyScroll();
s.on('down', function() {
console.log('down...');
});