Tiny Scroll
A tiny scrolling library for your in-page links.
- Plain old vanilla JS.
- Just 1.7kb gzipped.
- Uses
requestAnimationFrame
for great performance. - Does not block user scroll.
Examples
Codepen
Alternatively, take a look in /examples
.
Installation
npm install tinyscroll
init
function
The The Tiny Scroll init
function looks for all in page links with a certain class name (by default js-tinyscroll
), and replaces the default click behaviour with
a smooth scrolling action.
Usage
Scroll to Hello World section Hello world
tinyscroll
Options
The init
function can optionally take an object as the first argument, that
may include the following properties.
className
The class name that Tiny Scroll uses to locate links.
Defaults to js-tinyscroll
.
tinyscroll
duration
The scroll duration in milliseconds. Defaults to 2000.
tinyscroll
ease
The tweening function that is used to ease scroll position.
Defaults to easeInOutQuint
. Only easeInOutQuint
is built
in. Using this property will require you to import and use
a function from
tween-functions.
tinyscroll
onStart
A callback function triggered when scroll starts.
tinyscroll
onComplete
A callback function triggered when scroll is complete.
tinyscroll
onCancel
A callback function triggered if scroll is cancelled by user.
tinyscroll
scrollTo
function
The The scrollTo
function is used to smoothly scroll to any element within a page. A target DOM node must be passed into the scrollTo
function as the first argument.
Usage
Scroll to Hello World section Hello world
const trigger = documentconst target = document trigger
Options
The scrollTo
function can optionally take an object as the second argument, that may include the following properties.
duration
The scroll duration in milliseconds. Defaults to 2000.
tinyscroll
ease
The tweening function that is used to ease scroll position.
Defaults to easeInOutQuint
. Only easeInOutQuint
is built
in. Using this property will require you to import and use
a function from
tween-functions.
tinyscroll
offset
The number of pixels to offset the scroll to endpoint by. Defaults to 0.
tinyscroll
onStart
A callback function triggered when scroll starts.
tinyscroll
onComplete
A callback function triggered when scroll is complete.
tinyscroll
onCancel
A callback function triggered if scroll is cancelled by user.
tinyscroll
Data attributes
It can be useful to override Tiny Scroll's options on a case-by-case basis.
duration
In the following case the duration will be 500ms.
The tinyscroll
duration option is overridden by the
data-duration
attribute on the anchor element.
Scroll to Hello World section Hello world
tinyscroll
Browser support
Tiny Scroll is packaged with Babel, and
makes use of Array.from
.
If you want Tiny Scroll to work on browsers that don't support
this method (e.g. IE11), then you will need to
polyfill Array.from
before calling tinyscroll
.