@jfm/scrollspy
v0.1.1
Published
Scrollspy using IntersectionObserver
Downloads
57
Maintainers
Readme
scrollspy
Zero dependencies scrollspy library using the Intersection Observer API.
Install
npm install @jfm/scrollspy
Usage
import scrollspy from '@jfm/scrollspy'
// or
// import { observe, unobserve } from '@jfm/scrollspy'
const el = document.getElementById('section')
function callback(el) {
console.log('scrolled to', el)
}
scrollspy.observe(el, callback)
APIs
observe
observe(element, callback)
Observe the element
, call the callback
when it comes into the viewport.
You can call it multiple times with different callbacks.
unobserve
unobserve(element, [callback])
Unobserve the element
with callback
. If callback
is not defined, remove all the callbacks.