Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New plugin: onscroll component #28

Open
sebnitu opened this issue Jun 20, 2019 · 1 comment
Open

New plugin: onscroll component #28

sebnitu opened this issue Jun 20, 2019 · 1 comment

Comments

@sebnitu
Copy link
Owner

sebnitu commented Jun 20, 2019

A JavaScript component that can be used to update a component based on how far down the page something was scrolled. This is used in my personal website to show/hide navigational tools.

@sebnitu
Copy link
Owner Author

sebnitu commented Jun 20, 2019

Here's the JavaScript plugin as pulled from my personal website and Lost Woods Jekyll theme:

import u from 'vrembem/src/js/utility.js'

export default function() {

  'use strict'

  let api = {}
  let settings
  let defaults = {
    classTarget: 'onscroll',
    classActive: 'is-active'
  }

  let run = () => {
    const targets = document.querySelectorAll('.' + settings.classTarget)
    for (let target of targets) {
      const trigger = document.querySelector(target.dataset.trigger)
      if (trigger) {
        if (window.scrollY > (trigger.offsetTop + trigger.offsetHeight)) {
          target.classList.add( settings.classActive )
        } else {
          target.classList.remove( settings.classActive )
        }
      }
    }
  }

  api.init = (options) => {
    api.destroy()
    settings = u.extend( defaults, options || {} )
    run()
    document.addEventListener('scroll', run, false)
    document.addEventListener('touchmove', run, false)
  }

  api.destroy = () => {
    settings = null
    document.removeEventListener('scroll', run, false)
    document.removeEventListener('touchmove', run, false)
  }

  api.init()

  return api
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant