1.1.0 • Published 5 years ago

@earthpeople/scroll-spy v1.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

Scroll Spy

A library for subscribing to scroll events in the browser. Runs all your events in one requestAnimationFrame.

Installation

$ npm install @earthpeople/scroll-spy

or

$ yarn add @earthpeople/scroll-spy

Usage

// some-component.js
import { startScrollSpy } from "@earthpeople/scroll-spy";

const subscribe = startScrollSpy();

let scrolledPast100 = false;

subscribe("scroll", event => {
  if (event.scrollTop > 100) {
    scrolledPast100 = true;
  }
});

startScrollSpy will only start the event loop if it's not already running, but it will always return the subscription function.

Events

EventReturnsFires
scrollEventObjectOn document scroll
scrollYscrollTop, scrollDeltaOn vertical scroll
scrollXscrollLeft, scrollXDeltaOn horizontal scroll
scrollEndEventObjectWhen document stops scrolling
scrollReachedEndatBottom, scrollTop, scrollDirectionWhen scroll reaches end of document
scrollReachedTopatTop, scrollTop, scrollDirectionWhen scroll reaches top
scrollDirectionChangeEventObjectWhen scroll direction changes
resizeEventObjectOn window resize
resizeEndEventObjectWhen window stops resizing

EventObject

keytypevalue
scrollTopnumberPixels from top
scrollLeftnumberPixels from left
scrollDirectionstring"up" or "down"
atTopbooltrue if scroll position is 0 or less*
atBottombooltrue if scroll position is full scroll height or more*
scrollHeightnumberFull scroll height of the document
scrollDeltanumberDifference in scroll since last tick
scrollXDeltanumberDifference in horizontal scroll since last tick
vhnumberViewport height in pixels
vwnumberViewport width in pixels
orientationstring"landscape" or "portrait"

* This is to prevent false answers in browsers with elastic scroll, like Safari

Subscribing

startScrollSpy returns a subscribe function when called. Use that to add your listeners.

const subscribe = startScrollSpy();

You can also import the subscribe method directly.

import { subscribe } from '@earthpeople/scroll-spy`

You can listen to multiple event types in the same listener, but be aware that some event types only returns a select collection of values.

subscribe("scroll resize", ({ orientation }) => {
  console.log(orientation);
});

Unsubscribing

The subscribe method returns a new function when called. Use that function to unsubscribe.

const unsubscribe = subscribe("scrollReachedEnd", ({ scrollTop }) => {
  alert(`Wow! You scrolled ${scrollTop} pixels and reached the end`);

  // No need to listen to this event anymore
  unsubscribe();
});

You can also unsubscribe to all events or remove all listeners on a specific event

import { startScrollSpy, unsubscribeAll } from '@earthpeople/scroll-spy'

// add a few listeners
...

// unsubscribe from just the resize events
unsubscribeAll('resize')

// unsubscribe from ALL events
unsubscribeAll()

ES5

If you want to use a pre compiled es5-module, import from @earthpeople/scroll-spy/es5 instead.

1.1.0

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.0

5 years ago

0.1.0

5 years ago