4.0.0 • Published 3 years ago

scroll-animator v4.0.0

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

scroll-animator

npm version CI status

Smart, lightweight functions to animate browser scroll.

  • Scroll the page or a specific element.
  • Scroll vertically and horizontally.
  • Scroll to a target element or an arbitrary position, with an optional offset.
  • Scroll animations can be interrupted by the user or other scripts (no “fighting” animations).
  • Scroll animations adapt to a moving target; handy when loading affects layout.
  • Intuitive easeInOutCubic animation timing; a soft acceleration and deceleration.
  • < 1 kB bundle size, tested.
  • SSR friendly.

Setup

To install with npm, run:

npm install scroll-animator

Support

  • Node.js: ^12.20 || >= 14.13
  • Browsers: > 0.5%, not OperaMini all, not IE > 0, not dead

Consider polyfilling:

API

function animateScroll

Smoothly scrolls an element to a target position within the element. Scroll interference caused by the user or another script interrupts the animation.

ParameterTypeDescription
optionsobjectOptions.
options.containerHTMLElement? = document.scrollingElementContainer element to scroll.
options.targetXnumber?Target X position within the container, defaulting to the current position.
options.targetYnumber?Target Y position within the container, defaulting to the current position.
options.offsetXnumber? = 0Target X position offset.
options.offsetYnumber? = 0Target Y position offset.
options.durationnumber? = 500Total scroll animation duration in milliseconds.
options.onInterruptFunction?Callback to run if the scroll animation is interrupted.
options.onArriveFunction?Callback to run after scrolling to the target.

Examples

Ways to import.

import { animateScroll } from 'scroll-animator';
import animateScroll from 'scroll-animator/public/animateScroll.js';

Ways to require.

const { animateScroll } = require('scroll-animator');
const animateScroll = require('scroll-animator/public/animateScroll.js');

Horizontally scroll an element to a certain position.

animateScroll({
  container: document.getElementById('panner'),
  targetX: 400,
});

function scrollToElement

Scrolls a container to a target element, using animateScroll. The animation adapts to a moving target; handy when loading affects layout.

ParameterTypeDescription
optionsobjectOptions.
options.containerHTMLElement? = document.scrollingElementContainer element to scroll.
options.targetHTMLElementTarget element to scroll to.
options.offsetXnumber? = 0Target X position offset.
options.offsetYnumber? = 0Target Y position offset.
options.durationnumber? = 500Total scroll animation duration in milliseconds.
options.onInterruptFunction?Callback to run if the scroll animation is interrupted.
options.onArriveFunction?Callback to run after scrolling to the target.

Examples

Ways to import.

import { scrollToElement } from 'scroll-animator';
import scrollToElement from 'scroll-animator/public/scrollToElement.js';

Ways to require.

const { scrollToElement } = require('scroll-animator');
const scrollToElement = require('scroll-animator/public/scrollToElement.js');

Scroll the page to an element.

scrollToElement({
  target: document.getElementById('contact-us'),
});
4.0.0

3 years ago

3.0.0

4 years ago

2.0.0

5 years ago

1.1.0

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.1.0-alpha.5

8 years ago

0.1.0-alpha.4

8 years ago

0.1.0-alpha.3

8 years ago

0.1.0-alpha.2

8 years ago

0.1.0-alpha.1

8 years ago