3.5.2 • Published 2 years ago

scroll-js v3.5.2

Weekly downloads
4,365
License
MIT
Repository
github
Last release
2 years ago

build npm node minified downloads license

Scroll

A light-weight library that will allow you to scroll any html element using native javascript. In addition to providing extra scrolling features, this library also aims to be a polyfill for the scrollTo and scrollIntoView APIs and allows you to scroll using animations that are based loosely on the scrollOptions of the DOM specification. Manipulates native scroll properties so that native events fire appropriately and uses browser's animation frames for fast and smooth rendering.

Why use this over other scroll libraries and plugins?

Many other scroller libraries use absolutely positioning, css animations, transitions and other types of workarounds directly on the window.document, <html>, <body> and other elements to "fake" a scrolling effect in order to get the scroller to behave.

While this is clever, desktop and mobile devices (mobile mainly), heavily depend on the natural scroll events of these elements to do helpful things for the user. Like hiding the location url bar as you scroll down the window of the document (on mobile browsers), for instance. Or pausing heavy processes, until the user is done performing a task as to not interrupt them, or adding inertia or natural momentum when scrolling. So it's increasingly important that the scroll logic added to these elements is done in a way that lends nicely to these use cases, which is what this library does.

Benefits

  • pure, native javascript
  • returns Promises so you can do things when scrolling completes (async/await is supported)
  • no css transitions, animations or absolute positioning hacks
  • manually scroll to any portion of a page and detect when done
  • safe to use on the document.body element
  • supports scroll options from CSS DOM specification
  • battery-friendly -- uses minimal amount of CPU power (no processing on inactive tabs, and hidden elements!)
  • fast and smooth rendering (no choppiness)
  • does not hijack native browser functionality (i.e. inertia scrolling, momentum defaults)
  • native "onscroll" events can still be used (window.onscroll and Element.onscroll)
  • Both non-AMD and AMD compatible

Installation

You can install the library as a npm module by running the following command:

npm i scroll-js

Importing

After installing, you can either import the component into your file:

import { scrollTo, scrollIntoView } from 'scroll-js';

Or, you can reference any of the files made available in the dist folder in your html file.

<script src="node_modules/scroll-js/dist/scroll.js"></script>

Usage

In addition to the samples below, you can find more in the examples folder.

Scrolling the window

import { scrollTo } from 'scroll-js';

scrollTo(window, { top: 500 }).then(function () {
    // window has scrolled 500 pixels down the page
});

Scrolling an element

You can manually scroll any element on a page and optionally detect when done. Just make sure the element you want to scroll has:

  1. A specified height css property.
  2. css overflow property that is set to hidden.
  3. Content that extends beyond the specified height.

The following example scrolls the window (document body).

import { scrollTo } from 'scroll-js';
scrollTo(document.body, { top: 500 }).then(function () {
    //scrolling down 500 pixels has completed!
});

Scroll to an element

import { scrollIntoView } from 'scroll-js';
var myElement = document.body.getElementsByClassName('my-element')[0];
scrollIntoView(myElement, document.body, { behavior: 'smooth' }).then(
    function () {
        // done scrolling document's body to show myElement
    }
);

Scroll easing

You can scroll with easing using the behavior option of the scrollTo specification.

import { scrollTo } from 'scroll-js';
scrollTo(document.body, { top: 600, behavior: 'smooth' }).then(function () {
    // scrolled down 600 pixels smoothly
});

Easing is also supported simply by passing the easing option with an easing string that can be found in the src/scroll.ts file.

import { scrollTo } from 'scroll-js';
scrollTo(document.body, { top: 200, easing: 'ease-in-out' }).then(function () {
    // scrolled down 200 pixels, easing on beginning and end
});

Note that even though easing option is supported by this package, it is not guaranteed that it will be supported by the specification.

Detect scroll events

Listen in on native scroll events the same way you would if a user was scrolling with a mouse or touch event.

import { scrollTo } from 'scroll-js';
window.addEventListener('scroll', function () {
    // scrolling!
});
scrollTo(document.body, { top: 300 }); // scroll to trigger event

API Documentation

scrollTo(element, options)

OptionTypeDescription
elementHTMLElementThe element to scroll
optionsScrollToOptionsA set of scroll options (see writeup below) (i.e. {behavior: 'smooth', top: '20', left: '0''})

scrollTo Options

The scrollTo method allows a set of options which are synonymous with the ScrollToOptions of the CSS specification, but some additional ones are provided by this library until supported natively.

OptionTypeDescription
behaviorStringThe type of scroll behavior which can be set to auto or smooth. This is the recommended option since this is already natively supported. If this is set, all other options are ignored.
durationNumberThe number of milliseconds the scroll will take to complete
easingStringThe easing to use when scrolling. Only keyword values of the animation-timing-function are supported. But passing function values will eventually be supported also (ie. cubic-bezier(0.1, 0.7, 1.0, 0.1), steps(4, end), etc)

scrollIntoView(element, scroller, options)

OptionTypeDescription
elementHTMLElementThe element to scroll into the viewport
scrollerHTMLElementThe element to be scrolled (defaults to document.body)
optionsScrollIntoViewOptionsA set of scroll options to scroll the element into view (see writeup below) (i.e. {behavior: 'smooth', top: '20', left: '0''})

scrollIntoView Options

A set of ScrollIntoViewOptions can be passed to the scrollIntoView method.

OptionTypeDescription
behaviorStringThe type of scroll behavior which can be set to auto or smooth. Defaults to auto.

Examples

Code samples showing how to use this package can be found in the examples folder. To run them, pull down this project and

npm start

Which will make the examples available at http://localhost:9383/examples/.

Development

Run tests:

npm install
npm test

E2E Tests

The following runs E2E tests on the files in the examples folder.

npm run test:e2e
3.5.2

2 years ago

3.5.1

2 years ago

3.5.0

2 years ago

3.4.2

3 years ago

3.4.0

3 years ago

3.4.1

3 years ago

3.3.0

4 years ago

3.2.0

4 years ago

3.1.1

4 years ago

3.1.0

5 years ago

3.0.0

5 years ago

2.3.2

5 years ago

2.3.1

5 years ago

2.3.0

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

2.0.0-dev

6 years ago

1.9.1

6 years ago

1.9.0

6 years ago

1.8.8

6 years ago

1.8.7

6 years ago

1.8.6

6 years ago

1.8.5

6 years ago

1.8.4

6 years ago

1.8.3

6 years ago

1.8.2

6 years ago

1.8.1

6 years ago

1.8.0

6 years ago

1.7.1

7 years ago

1.7.0

7 years ago

1.6.0

7 years ago

1.5.1

7 years ago

1.4.4

7 years ago

1.4.3

7 years ago

1.4.2

7 years ago

1.4.1

7 years ago

1.4.0

7 years ago

1.3.0

7 years ago

1.2.12

7 years ago

1.2.10

7 years ago

1.2.9

7 years ago

1.2.8

7 years ago

1.2.7

7 years ago

1.2.6

7 years ago

1.2.5

7 years ago

1.2.4

7 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.5.2

8 years ago

0.5.1

8 years ago

0.5.0

8 years ago

0.4.0

9 years ago

0.3.10

9 years ago

0.3.9

9 years ago

0.3.8

9 years ago

0.3.7

9 years ago

0.3.6

9 years ago

0.3.3

9 years ago