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

Version Path
rails_admin_image_manager-0.1.34 app/assets/javascripts/rails_admin_image_manager/libs/lazyload.js
rails_admin_image_manager-0.1.33 app/assets/javascripts/rails_admin_image_manager/libs/lazyload.js
rails_admin_image_manager-0.1.31 app/assets/javascripts/rails_admin_image_manager/libs/lazyload.js
rails_admin_image_manager-0.1.30 app/assets/javascripts/rails_admin_image_manager/libs/lazyload.js
rails_admin_image_manager-0.1.3 app/assets/javascripts/rails_admin_image_manager/libs/lazyload.js
rails_admin_image_manager-0.1.29 app/assets/javascripts/rails_admin_image_manager/libs/lazyload.js
rails_admin_image_manager-0.1.28 app/assets/javascripts/rails_admin_image_manager/libs/lazyload.js
rails_admin_image_manager-0.1.27 app/assets/javascripts/rails_admin_image_manager/libs/lazyload.js
rails_admin_image_manager-0.1.26 app/assets/javascripts/rails_admin_image_manager/libs/lazyload.js
rails_admin_image_manager-0.1.25 app/assets/javascripts/rails_admin_image_manager/libs/lazyload.js
rails_admin_image_manager-0.1.24 app/assets/javascripts/rails_admin_image_manager/libs/lazyload.js
rails_admin_image_manager-0.1.23 app/assets/javascripts/rails_admin_image_manager/libs/lazyload.js
rails_admin_image_manager-0.1.22 app/assets/javascripts/rails_admin_image_manager/libs/lazyload.js
rails_admin_image_manager-0.1.21 app/assets/javascripts/rails_admin_image_manager/libs/lazyload.js
rails_admin_image_manager-0.1.20 app/assets/javascripts/rails_admin_image_manager/libs/lazyload.js
rails_admin_image_manager-0.1.15 app/assets/javascripts/rails_admin_image_manager/libs/lazyload.js