Sha256: 8ad56e9941e537af9cc1b649d3b2db491808fcbff18be22007de3189ad4ee941
Contents?: true
Size: 1.2 KB
Versions: 16
Compression:
Stored size: 1.2 KB
Contents
/** * * @param {Func} cb * @param {Object} options */ export default function Lazyload(cb, threshold = 120 ) { this.lastScroll = 0 this.cb = cb this.threshold = threshold this._boundHandleScroll = this.handleScroll.bind(this) } /** * Add event listener */ Lazyload.prototype.start = function() { // Keep initial scroll position this.lastScroll = getScrollTop() window.addEventListener('scroll', this._boundHandleScroll) } /** * Remove event listener */ Lazyload.prototype.stop = function() { window.removeEventListener('scroll', this._boundHandleScroll) } /** * Handle scroll */ Lazyload.prototype.handleScroll = function(event) { if (!this.ticking) { window.requestAnimationFrame(() => { // Get current scroll position const currentScroll = getScrollTop() // If user has scrolled down and more than threshold if (currentScroll - this.lastScroll >= this.threshold) { // Execute callback this.cb.call() // Keep new milestone this.lastScroll = currentScroll } this.ticking = false }) } this.ticking = true } function getScrollTop() { return window.pageYOffset || document.documentElement.scrollTop }
Version data entries
16 entries across 16 versions & 1 rubygems