easing-scroll 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +98 -0
- package/dist/easing-scroll.d.ts +20 -0
- package/dist/easing-scroll.js +1 -0
- package/dist/easing-scroll.mjs +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -0
- package/package.json +52 -0
- package/src/easing-scroll.ts +78 -0
- package/src/index.ts +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 βοΈ
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# easing-scroll
|
|
2
|
+
|
|
3
|
+
[](https://npmjs.org/package/easing-scroll)
|
|
4
|
+
|
|
5
|
+
βΏοΈ Smooth scrolling. [Demo](https://easing-scroll.vercel.app).
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install easing-scroll
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- π¦ Zero dependencies
|
|
16
|
+
- π Customize [easing function](https://easings.net)
|
|
17
|
+
- π« Abort scrolling ([AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal))
|
|
18
|
+
- π Waiting for animation to end
|
|
19
|
+
- βΈοΈ Supports vertical and horizontal scroll
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { easingScroll } from "easing-scroll";
|
|
25
|
+
|
|
26
|
+
const controller = new AbortController();
|
|
27
|
+
// Abort scrolling
|
|
28
|
+
// controller.abort(); β
|
|
29
|
+
|
|
30
|
+
const target = document.querySelector(".container");
|
|
31
|
+
|
|
32
|
+
const progress = await easingScroll(target, {
|
|
33
|
+
left: 0, // px
|
|
34
|
+
top: 300, // px
|
|
35
|
+
duration: 400, // ms
|
|
36
|
+
signal: controller.signal,
|
|
37
|
+
// π https://easings.net/#easeOutCubic
|
|
38
|
+
easing: (x) => 1 - Math.pow(1 - x, 3),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
if (progress === 1) {
|
|
42
|
+
console.log("Completed");
|
|
43
|
+
} else {
|
|
44
|
+
console.log("Aborted");
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Animation
|
|
49
|
+
|
|
50
|
+
Linear function `(t) => t` is used by default. Pass [easing](https://easings.net), if you want to change easing function.
|
|
51
|
+
`duration` is animation duration in milliseconds.
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
easingScroll(target, {
|
|
55
|
+
duration: 400, // ms
|
|
56
|
+
// π https://easings.net/#easeOutCubic
|
|
57
|
+
easing: (x) => 1 - Math.pow(1 - x, 3),
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Abort scrolling
|
|
62
|
+
|
|
63
|
+
Pass `signal` ([AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)),
|
|
64
|
+
if you want to abort scrolling.
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
const controller = new AbortController();
|
|
68
|
+
setTimeout(() => {
|
|
69
|
+
controller.abort();
|
|
70
|
+
}, 100);
|
|
71
|
+
|
|
72
|
+
const progress = await easingScroll(target, {
|
|
73
|
+
...,
|
|
74
|
+
signal: controller.signal,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
if (progress !== 1) {
|
|
78
|
+
console.log('Scrolling has been aborted.');
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
`progress` is a number from _0_ to _1_.
|
|
83
|
+
|
|
84
|
+
`1` - Scrolling is completed _100%_.
|
|
85
|
+
|
|
86
|
+
`<1` - Scrolling has been aborted and completed _x%_.
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
const progress = await easingScroll(target, {
|
|
90
|
+
...,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
if (progress !== 1) {
|
|
94
|
+
console.log('Scrolling has been aborted.');
|
|
95
|
+
} else {
|
|
96
|
+
console.log('Completed.');
|
|
97
|
+
}
|
|
98
|
+
```
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
type Px = number;
|
|
2
|
+
type Ms = number;
|
|
3
|
+
/**
|
|
4
|
+
* Percent is number 0 - 1
|
|
5
|
+
*
|
|
6
|
+
* 0 = 0%, 1 = 100%
|
|
7
|
+
*/
|
|
8
|
+
type Pct = number;
|
|
9
|
+
type Options = {
|
|
10
|
+
top: Px;
|
|
11
|
+
left: Px;
|
|
12
|
+
duration: Ms;
|
|
13
|
+
signal: AbortSignal;
|
|
14
|
+
/**
|
|
15
|
+
* @see Easing functions https://easings.net
|
|
16
|
+
*/
|
|
17
|
+
easing: (t: Pct) => Pct;
|
|
18
|
+
};
|
|
19
|
+
export declare const easingScroll: <E extends Element>(target: E, { top, left, signal, duration, easing }: Partial<Options>) => Promise<Pct>;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const b=o=>o,g=(o,{top:l,left:t,signal:e,duration:s=0,easing:i=b})=>e!=null&&e.aborted?Promise.resolve(0):s?new Promise(c=>{const a=o.scrollTop,m=o.scrollLeft,u=performance.now();let n;const v=()=>(performance.now()-u)/s,f=()=>{cancelAnimationFrame(n);const r=v();c(r)};e==null||e.addEventListener("abort",f);const p=()=>{const r=v(),d=l===void 0?void 0:a+(l-a)*i(r),T=t===void 0?void 0:m+(t-m)*i(r);r<1?(o.scrollTo({top:d,left:T}),n=requestAnimationFrame(p)):(o.scrollTo({top:l,left:t}),e==null||e.removeEventListener("abort",f),c(1))};n=requestAnimationFrame(p)}):(o.scrollTo({top:l,left:t}),Promise.resolve(1));exports.easingScroll=g;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const u=o=>o,L=(o,{top:t,left:r,signal:n,duration:s=0,easing:i=u})=>n?.aborted?Promise.resolve(0):s?new Promise(a=>{const c=o.scrollTop,m=o.scrollLeft,f=performance.now();let l;const d=()=>(performance.now()-f)/s,p=()=>{cancelAnimationFrame(l);const e=d();a(e)};n?.addEventListener("abort",p);const v=()=>{const e=d(),T=t===void 0?void 0:c+(t-c)*i(e),b=r===void 0?void 0:m+(r-m)*i(e);e<1?(o.scrollTo({top:T,left:b}),l=requestAnimationFrame(v)):(o.scrollTo({top:t,left:r}),n?.removeEventListener("abort",p),a(1))};l=requestAnimationFrame(v)}):(o.scrollTo({top:t,left:r}),Promise.resolve(1));export{L as easingScroll};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { easingScroll } from "./easing-scroll";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const b=o=>o,g=(o,{top:l,left:t,signal:e,duration:s=0,easing:i=b})=>e!=null&&e.aborted?Promise.resolve(0):s?new Promise(c=>{const a=o.scrollTop,m=o.scrollLeft,u=performance.now();let n;const v=()=>(performance.now()-u)/s,f=()=>{cancelAnimationFrame(n);const r=v();c(r)};e==null||e.addEventListener("abort",f);const p=()=>{const r=v(),d=l===void 0?void 0:a+(l-a)*i(r),T=t===void 0?void 0:m+(t-m)*i(r);r<1?(o.scrollTo({top:d,left:T}),n=requestAnimationFrame(p)):(o.scrollTo({top:l,left:t}),e==null||e.removeEventListener("abort",f),c(1))};n=requestAnimationFrame(p)}):(o.scrollTo({top:l,left:t}),Promise.resolve(1));exports.easingScroll=g;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const u=o=>o,L=(o,{top:t,left:r,signal:n,duration:s=0,easing:i=u})=>n?.aborted?Promise.resolve(0):s?new Promise(a=>{const c=o.scrollTop,m=o.scrollLeft,f=performance.now();let l;const d=()=>(performance.now()-f)/s,p=()=>{cancelAnimationFrame(l);const e=d();a(e)};n?.addEventListener("abort",p);const v=()=>{const e=d(),T=t===void 0?void 0:c+(t-c)*i(e),b=r===void 0?void 0:m+(r-m)*i(e);e<1?(o.scrollTo({top:T,left:b}),l=requestAnimationFrame(v)):(o.scrollTo({top:t,left:r}),n?.removeEventListener("abort",p),a(1))};l=requestAnimationFrame(v)}):(o.scrollTo({top:t,left:r}),Promise.resolve(1));export{L as easingScroll};
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "easing-scroll",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "βΏοΈ Smooth scrolling",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"source": "src/index.ts",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"module": "dist/index.mjs",
|
|
9
|
+
"types": "dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"require": "./dist/index.js",
|
|
13
|
+
"import": "./dist/index.mjs",
|
|
14
|
+
"default": "./dist/index.mjs"
|
|
15
|
+
},
|
|
16
|
+
"./easing-scroll": {
|
|
17
|
+
"require": "./dist/easing-scroll.js",
|
|
18
|
+
"import": "./dist/easing-scroll.mjs",
|
|
19
|
+
"default": "./dist/easing-scroll.mjs"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"check-types": "tsc",
|
|
24
|
+
"clean": "rm -r dist/*",
|
|
25
|
+
"build": "npm run clean && rollup -c",
|
|
26
|
+
"release": "npm run check-types && npm publish"
|
|
27
|
+
},
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://github.com/faustienf/easing-scroll.git"
|
|
31
|
+
},
|
|
32
|
+
"keywords": [
|
|
33
|
+
"scroll",
|
|
34
|
+
"smooth",
|
|
35
|
+
"smooth-scroll",
|
|
36
|
+
"easing"
|
|
37
|
+
],
|
|
38
|
+
"author": "@faustien",
|
|
39
|
+
"license": "MIT",
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/faustienf/easing-scroll/issues"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://github.com/faustienf/easing-scroll#readme",
|
|
44
|
+
"prettier": {},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"esbuild": "^0.16.12",
|
|
47
|
+
"rollup": "^3.9.0",
|
|
48
|
+
"rollup-plugin-esbuild": "^5.0.0",
|
|
49
|
+
"rollup-plugin-typescript2": "^0.34.1",
|
|
50
|
+
"typescript": "^4.9.4"
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
type Px = number;
|
|
2
|
+
type Ms = number;
|
|
3
|
+
/**
|
|
4
|
+
* Percent is number 0 - 1
|
|
5
|
+
*
|
|
6
|
+
* 0 = 0%, 1 = 100%
|
|
7
|
+
*/
|
|
8
|
+
type Pct = number;
|
|
9
|
+
|
|
10
|
+
type Options = {
|
|
11
|
+
top: Px;
|
|
12
|
+
left: Px;
|
|
13
|
+
duration: Ms;
|
|
14
|
+
signal: AbortSignal;
|
|
15
|
+
/**
|
|
16
|
+
* @see Easing functions https://easings.net
|
|
17
|
+
*/
|
|
18
|
+
easing: (t: Pct) => Pct;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const linear = (t: number): number => t;
|
|
22
|
+
|
|
23
|
+
export const easingScroll = <E extends Element>(
|
|
24
|
+
target: E,
|
|
25
|
+
{ top, left, signal, duration = 0, easing = linear }: Partial<Options>
|
|
26
|
+
): Promise<Pct> => {
|
|
27
|
+
if (signal?.aborted) {
|
|
28
|
+
return Promise.resolve(0);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (!duration) {
|
|
32
|
+
target.scrollTo({ top, left });
|
|
33
|
+
return Promise.resolve(1);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return new Promise<Pct>((resolve) => {
|
|
37
|
+
const startTop = target.scrollTop;
|
|
38
|
+
const startLeft = target.scrollLeft;
|
|
39
|
+
const startTimestamp = performance.now();
|
|
40
|
+
let ramID: number;
|
|
41
|
+
|
|
42
|
+
const getProgress = (): Pct => {
|
|
43
|
+
const elapsed = performance.now() - startTimestamp;
|
|
44
|
+
return elapsed / duration;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const abortHandler = () => {
|
|
48
|
+
cancelAnimationFrame(ramID);
|
|
49
|
+
const progress = getProgress();
|
|
50
|
+
resolve(progress);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
signal?.addEventListener("abort", abortHandler);
|
|
54
|
+
|
|
55
|
+
const tick = () => {
|
|
56
|
+
const progress = getProgress();
|
|
57
|
+
const tickTop =
|
|
58
|
+
top === undefined
|
|
59
|
+
? undefined
|
|
60
|
+
: startTop + (top - startTop) * easing(progress);
|
|
61
|
+
const tickLeft =
|
|
62
|
+
left === undefined
|
|
63
|
+
? undefined
|
|
64
|
+
: startLeft + (left - startLeft) * easing(progress);
|
|
65
|
+
|
|
66
|
+
if (progress < 1) {
|
|
67
|
+
target.scrollTo({ top: tickTop, left: tickLeft });
|
|
68
|
+
ramID = requestAnimationFrame(tick);
|
|
69
|
+
} else {
|
|
70
|
+
target.scrollTo({ top, left });
|
|
71
|
+
signal?.removeEventListener("abort", abortHandler);
|
|
72
|
+
resolve(1);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
ramID = requestAnimationFrame(tick);
|
|
77
|
+
});
|
|
78
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { easingScroll } from "./easing-scroll";
|