0.0.8-beta • Published 4 years ago

scrolltween v0.0.8-beta

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

scrolltween

npm.io npm.io npm.io

ScrollTween is a utility that enables binding of properties of DOM elements to the scroll event and tweening of these properties accordingly.

Installation

Setup via NPM

npm install scrolltween --save

Setup via Yarn

yarn add scrolltween

ScrollTween ships with four build flavors:

  • UMD: index.umd.js
  • AMD: index.amd.js
  • CommonJS: index.cjs.js
  • ES6 module: index.esm.js

In browser environments, include index.umd.js in the body tag of an html page as shown below:

<script src="dist/index.umd.js"></script>

In node environments, however, import the module as follows.

const ScrollTween = require("scrolltween");

or

import ScrollTween from "scrolltween";

ScrollTween ships with Typescript definitions for write-time type checking.

Live demo

Please see live demo at CodeSandbox.

Quickstart

import ScrollTween from "scrolltween";

ScrollTween.define([
	{
		selector: ".element",
		duration: 20,
		props: {
			rotate: 90,
		},
	},
]).start();

Key Concepts

TweenableProperties

TweenableProperties is an object that defines the properties of a DOM element that are to be tweened in an action. The following properties can be tweened.

PropertyTypeValue type
translatenumberincrement
translateXnumberincrement
translateYnumberincrement
translateZnumberincrement
scalenumberincrement
scaleXnumberincrement
scaleYnumberincrement
scaleZnumberincrement
rotatenumberincrement
rotateXnumberincrement
rotateYnumberincrement
rotateZnumberincrement
opacitynumberfinal
colorstringfinal
backgroundColorstringfinal
fontSizenumberfinal
  • increment: Provide value to be spread across tween range.
  • final: Provide value at the end of tweening.

Note: Unit of fontSize is px.

ScrollTweenAction

A ScrollTweenAction describes a DOM element to be tweened, and the tweening configuration. It takes the following shape:

PropertyTypeDescription
selectorstringThe DOM element whose properties are to be tweened.
trigger?stringWatch this DOM element instead to trigger tween start. Uses the selector element by default.
duration?numberThe total duration of a tweening action. It's defined in vh, that is, 1 unit of the viewport height.
delay?numberA delay that defines the duration from when the trigger element appears in the viewport to when the actual tweening starts. It's also defined in vh.
props?TweenablePropertiesThe properties of the DOM element that are to be tweened.

ScrollTweenInstance

A ScrollTweenInstance exposes the following methods:

MethodDescription
start()Call this method on a ScrollTweenInstance to begin watching for changes in vertical scroll and start tweening the DOM element.
refresh()Reinitialize the tweening process, typically when the DOM properties of the trigger element have been modified externally.
destroy()Destroy the ScrollTweenInstance and stop watching for scroll changes.

Overview

ScrollTween provides the following top-level methods:

  • define
  • sequence
  • parallel
  • staggered
  • fromChildren
  • raw

define

ScrollTween.define(actions) allows initial configuration of tweening actions. It returns a ScrollTweenInstance.

ParameterTypeDescription
actionsArray<ScrollTweenAction>A list of actions representing DOM elements to be tweened and their respective tweening configurations.

sequence

ScrollTween.sequence(trigger, actions, delay?) allows definition of tweening actions that are to be executed sequentially. It returns an array of ScrollTweenAction.

ParameterTypeDescription
triggerstringSelector for the DOM element to watch to know when to start sequential tweening.
actionsArray<ScrollTweenAction>A list of actions representing DOM elements to be tweened and their respective tweening configurations.
delay?numberAn optional length of scroll time that should be added before tweening starts. It's defined in vh.

parallel

ScrollTween.parallel(trigger, actions, delay?) allows definition of tweening actions that are to be executed in parallel. It returns an array of ScrollTweenAction.

ParameterTypeDescription
triggerstringSelector for the DOM element to watch to know when to start sequential tweening.
actionsArray<ScrollTweenAction>A list of actions representing DOM elements to be tweened and their respective tweening configurations.
delay?numberAn optional length of scroll time that should be added before tweening starts. It's defined in vh.

staggered

ScrollTween.staggered(trigger, stagger, actions, delay?) allows definition of tweening actions that are to be executed in parallel, but with successive delays. It returns an array of ScrollTweenAction.

ParameterTypeDescription
triggerstringSelector for the DOM element to watch to know when to start sequential tweening.
staggernumberThe delay between each action.
actionsArray<ScrollTweenAction>A list of actions representing DOM elements to be tweened and their respective tweening configurations.
delay?numberAn optional length of scroll time that should be added before tweening starts. It's defined in vh.

fromChildren

ScrollTween.fromChildren(parent, duration, props) is a utility function that generates a list of ScrollTweenAction from the direct children of a DOM element. It returns an array of ScrollTweenAction.

ParameterTypeDescription
parentstringSelector for the parent DOM element.
durationnumberDuration to be applied to each action
propsTweenableProperties | Array<TweenableProperties>An object or an Array containing properties to be tweened. When an array of TweenableProperties is provided, the index of the child DOM element is used to get its TweenableProperties from the array.

raw

ScrollTween.raw(action, callback) calls a callback with a value between 0 and 1, representing the tween state. This will typically be used for tweening instances not covered by ScrollTween. It's optimized and only calls the callback with a new value only if the value has changed. It returns a ScrollTweenInstance that is auto-started and can also be refreshed and destroyed.

ParameterTypeDescription
actionPartial<ScrollTweenAction>Defines trigger, duration and optionally delay.
callback(value: number) => voidA function to be called with the tween value (between 0 and 1) whenever it changes.

License

MIT © christiandrey