Last active
July 25, 2018 20:58
-
-
Save MeyCry/5fb9c52d0906bbe26dc2daf953ba849c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var OnScroll = (function () { | |
function OnScroll(config) { | |
if (!config) { | |
config = {} | |
} | |
this.listeners = []; | |
this.timeout = config.timeout || 300; | |
var handler = _scrollHandler.bind(this); | |
if (window.addEventListener) { | |
window.addEventListener('scroll', handler, false); | |
} else if (window.attachEvent) { | |
window.attachEvent('onscroll', handler); | |
} | |
} | |
var _timer = null; | |
function _scrollHandler(ev) { | |
clearTimeout(_timer); | |
var self = this; | |
_timer = setTimeout(function () { | |
var topIndent = document.documentElement.scrollTop || document.body.scrollTop, | |
w = window, | |
d = document, | |
e = d.documentElement, | |
g = d.body, | |
// x = w.innerWidth || e.clientWidth || g.clientWidth, | |
windowHeight = w.innerHeight || e.clientHeight || g.clientHeight, | |
bottomIndent = topIndent + windowHeight; | |
self.listeners.forEach(function (cb) { | |
cb(topIndent, bottomIndent); | |
}); | |
}, this.timeout); | |
} | |
OnScroll.prototype.onScroll = function (cb) { | |
this.listeners.push(cb); | |
}; | |
OnScroll.prototype.offScroll = function (cb) { | |
this.listeners = this.listeners.filter(function (listener) { | |
return cb !== listener; | |
}) | |
}; | |
return OnScroll; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment