1.1.1 • Published 1 year ago

basic-scroll-animations v1.1.1

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Basic Scroll Animations

This is a small and simple library for creating scroll animations, inspired by ScrollMagic.

Installation

npm install basic-scroll-animations

Usage

import { updateOnScroll,triggerOnScroll,elementBottom } from 'basic-scroll-animations';

updateOnScroll('.myElement(s)')
    .from(0,{opacity:0})
    .to(1,{opacity:1})
    .using({easing:'easeInOut'});

triggerOnScroll('.myElement(s)')
    .after(elementBottom('.myElement'))
    .applyClass('myClass');

API

Entry point: updateOnScroll(selector)

Registers selected element(s) to be updated on scroll using the returned Animation object.

ParamTypeDescription
selectorStringCSS selector for the element(s) to be updated on scroll.

Returns: Animation

Entry point: triggerOnScroll(selector)

Registers selected element(s) to be applied an effect on scroll using the returned Trigger object.

ParamTypeDescription
selectorStringCSS selector for the element(s) to be updated on scroll.

Returns: Trigger

Position calculation functions:

elementTop(selector) Returns the top position of the element(s) relative to the document in scroll percentage.

elementBottom(selector) Returns the bottom position of the element(s) relative to the document in scroll percentage.

elementHeight(selector) Returns the height of the element(s) in scroll percentage.

Note: if the selector matches multiple elements, the functions will return the data of the first element.

It is recommended to use these functions to calculate the position of the element(s) on scroll. Example:

const start = elementBottom('.myElement(s)');
const finish = elementBottom('.myElement(s)') + elementHeight('.myElement(s)');

updateOnScroll('.myElement(s)')
    .from(start,{opacity:0})
    .to(finish,{opacity:1});

Animation

The Animation object is returned by the entry point functions and is used to define the animation for the element(s) registered with it. The from and to functions are used to define the 2 styles that will be used to interpolate the element(s) style on scroll. !Should only be initalized by an entry function.

from Sets the initial style of the element(s).

ParamTypeDescription
percentNumberThe percentage (between 0 and 1) at which the element(s) should start being animated.
styleStyleThe starting style to be applied to the element(s).

Returns: Animation

to Sets the final style of the element(s).

ParamTypeDescription
percentNumberThe percentage (between 0 and 1) at which the element(s) should be fully animated.
styleStyleThe final style to be applied to the element(s).

Returns: Animation

using Sets the interpolation function to be used to interpolate the element(s) style on scroll.

ParamTypeDescription
optionsOptionsThe options to be used to interpolate the element(s) style on scroll.

Returns: Animation

Style

The Style object is used to define the style of the element(s) to be animated. The Style object currently supports the following properties:

PropertyTypeValuesDescription
opacityNumber0-1The opacity of the element(s).
translateXNumberanyThe X translation of the element(s).
translateYNumberanyThe Y translation of the element(s).
scaleXNumberanyThe X scale of the element(s).
scaleYNumberanyThe Y scale of the element(s).

Options

The Options object is used to define the behaviour of the animation. The Options object currently supports the following properties:

PropertyTypeValuesDescription
easingStringlineareaseIneaseOuteaseInOutThe easing function to be used to interpolate the element(s) style on scroll.

Trigger

The Trigger object is returned by an entry function and is used to apply effects to the selected elements at a specific scroll position. !Should only be initalized by an entry function.

after Applies the given style to the selected elements after the given scroll position.

ParamTypeDescription
percentNumberThe percentage (between 0 and 1) at which the style should be applied to the element(s).

Returns: Trigger

before Applies the given style to the selected elements before the given scroll position.

ParamTypeDescription
percentNumberThe percentage (between 0 and 1) at which the style should be applied to the element(s).

Returns: Trigger

applyClass

ParamTypeDescription
classNameStringThe class name to be applied to the element(s).

Returns: Trigger

removeClass

ParamTypeDescription
classNameStringThe class name to be removed from the element(s).

Returns: Trigger