0.4.0 • Published 5 years ago

scroll-callback v0.4.0

Weekly downloads
5
License
ISC
Repository
github
Last release
5 years ago

scroll-callback

A scroll event listener module

Install

yarn add scroll-callback

Description

This module can be used to fire a callback on a page scroll. Many scenarios are handled.

You can:

  • Fire a callback when an element reaches the top of the screen
  • Fire a callback when an element reaches the bottom of the screen
  • Fire a callback and return the element everytime the page scrolls
  • Fire a callback when multiple elements reaches the top of the screen
  • ...more!

This is element focused and can be used in many ways.

A work-in-progress JS module.

Built by: Mario Lo

Example

import { 
    scrollCallback, 
    scrollCallbackWithElement, 
    scrollCallbackWithElements,
    callbackAtElement, 
    callbackAtElements,
    callbackAtElementSurface 
} from 'scroll-callback';

(() => {
    scrollCallback(callback(), 1000);
    scrollCallbackWithElement(callback(), 1000, 'html-id');
    scrollCallbackWithElements(callback(), 1500, 'html-class');
    callbackAtElement(callback(), 1500, 'html-id');
    callbackAtElements(callback(), 1500, 'html-class');
    callbackAtElementSurface(callback(), 1500, 'html-id');
})();

Functions

1. scrollCallback(callback, waitDuration)

Fires a callback as the page is scrolling.

Does not remove the event listener.

2. scrollCallbackWithElement(callback, waitDuration, elementId)

Fires the callback as the page is scrolling and returns the element in the callback.

Does not remove the event listener.

Returns the element in callback

3. scrollCallbackWithElements(callback, waitDuration, elementClass)

Pass in a class.

Fires the callback as the page is scrolling and returns the elements in the callback.

Does not remove the scroll event listener.

Returns a elements in callback

4. callbackAtElement(callback, waitDuration, elementId)

Fires the callback when the element reaches the top of the page.

Removes the event listener.

Returns the element in callback

5. callbackAtElements(callback, waitDuration, elementClass)

Pass in class.

Fires the callback when the element reaches the top of the page.

Remove the event listener when all elements have been "called back"

Returns the elements in callback

6. callbackAtElementSurface(callback, waitDuration, elementId)

Fires the callback when the element reaches the bottom of the page.

Remove the scroll event listener

Returns the element in callback

7. getElementByClass(className)

Gets the elements via class name. Calls querySelectorAll().

Returns a static nodeList

Parameters

Parameter NameTypeRequiredDefault ValueDescription
callbackfunctiontruenullA function to be called on scroll
waitDurationnumber or null for defaultfalse50Wait duration in between callback
elementIdstringfalsenullThe id of the element we want the position of
elementClassstringfalsetrueThe id of the element we want the position of
classNamestringfalsetrueClass name of elements we want

callback

type: function

This function will be called as the window is scrolling.

waitDuration

type: number

This is a wait time in milli-seconds. This is provided to give you custom timing to fire your callback.

elementId

type: string

An element ID in the DOM. If supplied, the element be returned to the callback.

className

type: string

This should be the class name of the elements we want.

Contributing

Please open a new issue and mention me to get the ball rolling. Thanks!

Future Features

  • Ability to handle multiple elements

Feature Request

Please message hello@websitesbymario.com for feature requests.

Bug Report

Please message hello@websitesbymario.com to report bugs.

0.4.0

5 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.2

6 years ago

0.1.11

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.40

6 years ago

0.0.38

6 years ago

0.0.37

6 years ago

0.0.36

6 years ago

0.0.35

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago