11[ ![ GitHub release] ( https://img.shields.io/github/release/three11/animate-top-offset.svg )] ( https://github.com/three11/animate-top-offset/releases/latest )
22[ ![ GitHub issues] ( https://img.shields.io/github/issues/three11/animate-top-offset.svg )] ( https://github.com/three11/animate-top-offset/issues )
33[ ![ GitHub last commit] ( https://img.shields.io/github/last-commit/three11/animate-top-offset.svg )] ( https://github.com/three11/animate-top-offset/commits/master )
4- [ ![ Github file size] ( https://img.shields.io/github/size/three11/animate-top-offset/dist/animate-top-offset.min.js.svg )] ( https://github.com/three11/animate-top-offset/ )
54[ ![ Build Status] ( https://travis-ci.org/three11/animate-top-offset.svg?branch=master )] ( https://travis-ci.org/three11/animate-top-offset )
65[ ![ npm] ( https://img.shields.io/npm/dt/@three11/animate-top-offset.svg )] ( https://www.npmjs.com/package/@three11/animate-top-offset )
76[ ![ npm] ( https://img.shields.io/npm/v/@three11/animate-top-offset.svg )] ( https://www.npmjs.com/package/@three11/animate-top-offset )
87[ ![ Analytics] ( https://ga-beacon.appspot.com/UA-83446952-1/github.com/three11/animate-top-offset/README.md )] ( https://github.com/three11/animate-top-offset/ )
98
109# Animate Top Offset
1110
12- Vanilla JS animated scroll to a given offset.
11+ > Scroll a container to a specific Y offset
1312
1413## Install
1514
2322yarn add @three11/animate-top-offset
2423```
2524
26- or
27-
28- Just download this repository and link the files located in dist folder:
29-
30- ``` html
31- <script src =" path-to-animate-top-offset/dist/animate-top-offset.min.js" ></script >
32- ```
33-
34- or
35-
36- Include it from Unpkg CDN
37-
38- ``` html
39- <script src =" //unpkg.com/@three11/animate-top-offset/dist/animate-top-offset.min.js" ></script >
40- ```
41-
4225## Usage
4326
4427First, ` import ` the module:
4528
46- ``` javascript
29+ ``` ts
4730import animateTopOffset from ' @three11/animate-top-offset' ;
4831```
4932
5033Then use the module:
5134
52- #### With one element
35+ ### With one element
5336
54- ``` javascript
37+ ``` ts
5538const button = document .getElementById (' button' );
5639
5740button .addEventListener (' click' , event => {
@@ -64,9 +47,9 @@ button.addEventListener('click', event => {
6447});
6548```
6649
67- #### With many elements
50+ ### With many elements
6851
69- ``` javascript
52+ ``` ts
7053const buttons = document .querySelectorAll (' .js-scroll-to' );
7154
7255// Instead of Array.from you can spread the buttons: [...buttons]
@@ -77,14 +60,14 @@ Array.from(buttons).forEach(button => {
7760 const href = event .target .getAttribute (' href' );
7861 const offset = doc .querySelector (href ).offsetTop ;
7962
80- animateTopOffset (offset);
63+ animateTopOffset ({ offset } );
8164 });
8265});
8366```
8467
8568** The examples above assume that you have a modern ES6 setup installed and configured (Webpack, Babel, etc). If not you can always fallback to ES5:**
8669
87- ``` javascript
70+ ``` ts
8871const buttons = document .querySelectorAll (' .js-scroll-to' );
8972
9073[].forEach .call (buttons , function (button ) {
@@ -101,31 +84,44 @@ const buttons = document.querySelectorAll('.js-scroll-to');
10184
10285## Arguments
10386
104- The function accepts four arguments :
87+ The function accepts the following options :
10588
106- - ` offset `
107- - ` container `
108- - ` speed `
109- - ` easing `
89+ | Name | Type | Required | Description | Default value |
90+ | ----------- | ---------------------------------------------------- | -------- | ------------------------------ | ------------- |
91+ | ` offset ` | number | false | Offset to scroll to | 0 |
92+ | ` container ` | ` HTMLElement ` \| ` Window ` | false | The element to scroll | window |
93+ | ` speed ` | number | false | Speed of the scroll animation | 200 |
94+ | ` easing ` | 'easeOutSine' \| 'easeInOutSine' \| 'easeInOutQuint' | false | Easing of the scroll animation | 'easeOutSine' |
95+ | ` easings ` | Record<string, (pos: number) => number> | false | List of easing equations | See below |
11096
111- ``` javascript
112- animateTopOffset (0 , window , 2000 , ' easeOutSine' );
97+ ``` ts
98+ animateTopOffset ({ offset: 0 , container: window , speed: 2000 , easing: ' easeOutSine' , easings: easingEquations } );
11399```
114100
115- Default values are:
101+ ** Calling the function with the default values (` animateTopOffset() ` ) will scroll the window back to top.**
102+
103+ ## Easings
116104
117- - ` offset ` = 0
118- - ` container ` = ` window `
119- - ` speed ` = 2000
120- - ` easing ` = ` 'easeOutSine' `
105+ ` animateTopOffset ` provides the ability to specify a custom list of easing functions.
106+ The default one contains three easings: 'easeOutSine', 'easeInOutSine' and 'easeInOutQuint'.
121107
122- ** Calling the function with the default values ( ` animateTopOffset() ` ) will scroll the window back to top. **
108+ The shape of the list is the following:
123109
124- There are three built-in easing functions:
110+ ``` ts
111+ const easingEquations: Record <string , (pos : number ) => number > = {
112+ easeOutSine : (pos : number ) => Math .sin (pos * (Math .PI / 2 )),
113+ easeInOutSine : (pos : number ) => - 0.5 * (Math .cos (Math .PI * pos ) - 1 ),
114+ easeInOutQuint : (pos : number ) => {
115+ if ((pos /= 0.5 ) < 1 ) {
116+ return 0.5 * Math .pow (pos , 5 );
117+ }
118+
119+ return 0.5 * (Math .pow (pos - 2 , 5 ) + 2 );
120+ }
121+ };
122+ ```
125123
126- - ` 'easeOutSine' `
127- - ` 'easeInOutSine' `
128- - ` 'easeInOutQuint' `
124+ ** The easing argument should match one of the keys of the ` easings ` argument.`**
129125
130126## Demo
131127
0 commit comments