1.7.19 • Published 2 years ago

scroll-to-position v1.7.19

Weekly downloads
85
License
MIT
Repository
github
Last release
2 years ago

scroll-to-position

Animate scroll to either an x, y, or x and y position in any scrollable viewport with customisable easing.

Demo

GitHub release Build Status Codacy Badge

Install

You can install via npm or yarn.

npm

npm install --save scroll-to-position

yarn

yarn add scroll-to-position

Usage

Importing

You can import using ES6 imports.

import { ScrollTo } from 'scroll-to-position';

Arguments

ScrollTo accepts two arguments:

target: either a position (positionX, positionY) or a HTML Element (e.g. a reference to a div).

options (optional): an object of configuration options - see the options section below.

Examples

Position

ScrollTo([0, 100]);

Element

const myElement = document.querySelector('.MyElement');
ScrollTo(myElement);

Note: if you're using TypeScript you will most likely need to cast your element to a HTMLElement, as document.querySelector returns a Element type.

Callback

Calling ScrollTo returns a Promise, so it is possible to run your own code after scrolling has completed.

ScrollTo([0, 100]).then(() => console.log('Scrolling completed'));

If cancelOnUserScroll is enabled and the user interrupts scrolling, then the promise rejects and can be caught using Promise.prototype.catch

ScrollTo([0, 100])
  .then(() => console.log('Scrolling completed'))
  .catch(() => console.log('User interrupted scrolling'));

Options

When calling ScrollTo you can provide an options object, with values to override the defaults.

Option Parameters

ParameterTypeDescriptionDefault
scrollContainerHTMLElement or WindowThe area to scroll. e.g. window or a div with overflow autowindow
offsetPosition (number, number)If your target is a HTMLElement you may want to provide an offset to prevent scrolling right to the edge of the element0,0
durationDurationRange (number, number) or Duration (number)Either a DurationRange (minDuration, maxDuration) or a set duration (all values in milliseconds). If you provide a range the scroll distance (providing it's no less than the minDuration and no more than the maxDuration will be used as the duration)200, 5000
easingstringThe easing function the animation will use. The easing's available can be seen in my js-easing-functions repo (https://github.com/bameyrick/js-easing-functions)https://github.com/bameyrick/js-easing-functions
cancelOnUserScrollbooleanWhether the scroll animation should stop when the user scrollstrue
animatebooleanWhether ScrollTo should animate to the target, or jump straight there with no animationtrue
autoDurationMultipliernumberIf duration is to picked automatically (between DurationRange), multiply the distance to travel by this value to get the duration2
onlyScrollIfNotInViewbooleanOnly scroll if the target is not within the viewport of the scrollable area. If the provided target is a DOM element then it will check to see if the element is fully within the scroll area.false

Example of providing options

ScrollTo([0, 100], {
  duration: [300, 1000],
  easing: 'easeInOutSine',
});

Using the easing enum in TypeScript

import { ScrollTo, Easing } from 'scroll-to-position';

ScrollTo([0, 100], {
  duration: [300, 1000],
  easing: Easing.easeInQuad,
});

Extras

An autoScroll boolean is added to the window which you can use in your scroll event listeners to check if the window is being autoScrolled by this package.

1.17.8

2 years ago

1.7.17

2 years ago

1.7.18

2 years ago

1.7.19

2 years ago

1.7.12

2 years ago

1.7.13

2 years ago

1.7.14

2 years ago

1.7.15

2 years ago

1.7.16

2 years ago

1.7.10

3 years ago

1.7.11

3 years ago

1.7.9

4 years ago

1.7.8

4 years ago

1.7.7

4 years ago

1.7.5

4 years ago

1.7.4

4 years ago

1.7.3

4 years ago

1.7.2

4 years ago

1.7.1

4 years ago

1.7.0

4 years ago

1.6.2

5 years ago

1.6.1

5 years ago

1.6.0

5 years ago

0.0.1

5 years ago

1.5.3

6 years ago

1.5.2

6 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago