You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is an addon to [`scroll-into-view-if-needed`](https://www.npmjs.com/package/scroll-into-view-if-needed) that [ponyfills](https://ponyfill.com) smooth scrolling.
11
+
And while `scroll-into-view-if-needed` use the same default options as browsers and the spec does, this library is a bit more opinionated and include bonus features that help you build great UIs.
//If all you want is for all your users to have stuff smooth scroll into view
27
-
scrollIntoView(node, { behavior:'smooth' })
27
+
//`options.behavior` is set to `smooth` by default so you don't have to pass options like in `scroll-into-view-if-needed`
28
+
scrollIntoView(node)
28
29
29
-
// combine it with any of the other options
30
+
// combine it with any of the other options from 'scroll-into-view-if-needed'
30
31
scrollIntoView(node, {
31
-
behavior:'smooth',
32
32
scrollMode:'if-needed',
33
33
block:'nearest',
34
34
inline:'nearest',
35
35
})
36
36
37
-
//It returns a promise that is resolved when the animation is finished
37
+
// a promise is always returned to help reduce boilerplate
38
38
constsequence=async () => {
39
39
constslide=document.getElementById('slide-3')
40
40
// First smooth scroll to hero
@@ -44,6 +44,65 @@ const sequence = async () => {
44
44
}
45
45
```
46
46
47
+
## Polyfills
48
+
49
+
This library rely on `Promise` and `requestAnimationFrame`. This library does not ship with polyfills for these to keep bundlesizes as low as possible.
50
+
51
+
## API
52
+
53
+
Check the full API in [`scroll-into-view-if-needed`](https://github.com/stipsan/scroll-into-view-if-needed#api).
54
+
55
+
This library differs from the API in `scroll-into-view-if-needed` in the following ways:
56
+
57
+
* the second argument can't be a boolean, it must be either undefined or an object.
58
+
59
+
### scrollIntoView(target, [options]) => Promise
60
+
61
+
`scroll-into-view-if-needed` does not return anything, while this library will return a Promise that is resolved when all of the scrolling boxes are finished scrolling.
62
+
63
+
> The ability to cancel animations will be added in a future version.
This option deviates from `scroll-into-view-if-needed` in two ways.
74
+
75
+
* The default value is `smooth` instead of `auto`
76
+
* Using `smooth` adds it to browsers that miss it, and overrides the native smooth scrolling in the browsers that have it to ensure the scrolling is consistent in any browser.
77
+
78
+
#### duration
79
+
80
+
Type: `number`<br> Default: `300`
81
+
82
+
> Introduced in `v1.1.0`
83
+
84
+
This setting is not a hard limit.
85
+
The duration of a scroll differs depending on how many elements is scrolled, and the capabilities of the browser.
86
+
On mobile the browser might pause or throttle the animation if the user switches to another tab.
87
+
And there might be nothing to scroll.
88
+
No matter the scenario a Promise is returned so you can await on it.
89
+
90
+
#### ease
91
+
92
+
Type: `Function`
93
+
94
+
> Introduced in `v1.1.0`
95
+
96
+
The default easing is implemented like this, with `t` being in the range of `0` => `1`:
97
+
98
+
```typescript
99
+
scrollIntoView(node, {
100
+
ease: t=>0.5* (1-Math.cos(Math.PI*t)),
101
+
})
102
+
```
103
+
104
+
Here's more examples, like easeInCubic etc: https://gist.github.com/gre/1650294#file-easing-js
105
+
47
106
## Credits
48
107
49
108
*[smoothscroll](https://github.com/iamdustan/smoothscroll) for the reference implementation of smooth scrolling.
0 commit comments