@@ -35,29 +35,29 @@ The best utility package for smooth scrolling and centering elements in the page
35
35
## Basic usage explanation
36
36
37
37
``` js
38
- import { Scroll } from " scroll-utility" ;
38
+ import { Scroll } from " scroll-utility"
39
39
40
40
// declare scroll manager instance
41
41
42
- const scrollManager = new Scroll (); // create a scroll instance for window for scrolling the page
43
- const elementScrollManager = new Scroll (element); // for scrolling inside element instead of window
42
+ const scrollManager = new Scroll () // create a scroll instance for window for scrolling the page
43
+ const elementScrollManager = new Scroll (element) // for scrolling inside element instead of window
44
44
45
45
// start a scroll animation
46
- scrollManager .scrollBy (value, options); // offset current scroll position by "value"
47
- scrollManager .scrollToPosition (position, options); // scroll to position "position"
48
- scrollManager .scrollToPercent (percent, options); // scroll to position given by "percent"
49
- scrollManager .scrollToElement (element, options); // scroll to element "element"
46
+ scrollManager .scrollBy (value, options) // offset current scroll position by "value"
47
+ scrollManager .scrollToPosition (position, options) // scroll to position "position"
48
+ scrollManager .scrollToPercent (percent, options) // scroll to position given by "percent"
49
+ scrollManager .scrollToElement (element, options) // scroll to element "element"
50
50
51
51
// onScroll events
52
- scrollManager .onScroll = () => console .log (" scroll ocurred in scrollManager container" );
53
- scrollManager .onUtilityScroll = () => console .log (" this scroll utility did scrolled" );
54
- scrollManager .onUserScroll = () => console .log (" this scroll utility did not scrolled!" );
52
+ scrollManager .onScroll = () => console .log (" scroll ocurred in scrollManager container" )
53
+ scrollManager .onUtilityScroll = () => console .log (" this scroll utility did scrolled" )
54
+ scrollManager .onUserScroll = () => console .log (" this scroll utility did not scrolled!" )
55
55
56
56
// stopping animations
57
57
58
- scrollManager .stopAllAnimations (); // stop all animation in "scrollManager"
59
- const animation = scrollManager .scrollBy (10 ); // capture animation
60
- animation .stop (); // stop animation
58
+ scrollManager .stopAllAnimations () // stop all animation in "scrollManager"
59
+ const animation = scrollManager .scrollBy (10 ) // capture animation
60
+ animation .stop () // stop animation
61
61
```
62
62
63
63
## Installation
@@ -81,20 +81,20 @@ In this case `Scroll` will be a global variable as `ScrollUtility`
81
81
#### Scroll inside window (default behavior)
82
82
83
83
``` js
84
- import { Scroll } from " scroll-utility" ;
84
+ import { Scroll } from " scroll-utility"
85
85
86
- const windowScrollManager = new Scroll ();
86
+ const windowScrollManager = new Scroll ()
87
87
```
88
88
89
89
` windowScrollManager ` will be used to scroll the normal overflow in webpages
90
90
91
91
#### Scroll a div or any other html element
92
92
93
93
``` js
94
- import { Scroll } from " scroll-utility" ;
94
+ import { Scroll } from " scroll-utility"
95
95
96
- const htmlElement = document .getElementById (" some-html-element" );
97
- const elementScrollManager = new Scroll (htmlElement);
96
+ const htmlElement = document .getElementById (" some-html-element" )
97
+ const elementScrollManager = new Scroll (htmlElement)
98
98
```
99
99
100
100
` elementScrollManager ` will be used to scroll inside that specific element.
@@ -106,35 +106,35 @@ If `htmlElement` is _null_ or _undefined_ it will fallback to window scroll by d
106
106
107
107
``` js
108
108
// asuming windowScrollManager is declared
109
- windowScrollManager .scroll .toPosition (number, options); // number is a position in (px) in scroll
109
+ windowScrollManager .scroll .toPosition (number, options) // number is a position in (px) in scroll
110
110
111
111
// example:
112
- windowScrollManager .scroll .toPosition (273 );
112
+ windowScrollManager .scroll .toPosition (273 )
113
113
```
114
114
115
115
#### Scroll to a percent
116
116
117
117
``` js
118
- windowScrollManager .scroll .toPercent (percent, options); // number is a percent(from 0 to 100)
118
+ windowScrollManager .scroll .toPercent (percent, options) // number is a percent(from 0 to 100)
119
119
120
120
// example:
121
- windowScrollManager .scroll .toPercent (0 ); // scroll to the begining of the page
122
- windowScrollManager .scroll .toPercent (50 ); // to the middle
123
- windowScrollManager .scroll .toPercent (100 ); // the end
121
+ windowScrollManager .scroll .toPercent (0 ) // scroll to the begining of the page
122
+ windowScrollManager .scroll .toPercent (50 ) // to the middle
123
+ windowScrollManager .scroll .toPercent (100 ) // the end
124
124
```
125
125
126
126
#### Scroll to an element
127
127
128
128
``` js
129
- windowScrollManager .scroll .toElement (htmlElement, options);
129
+ windowScrollManager .scroll .toElement (htmlElement, options)
130
130
```
131
131
132
132
Here ` htmlElement ` should be an html element. _ null_ or _ undefined_ will scroll to the start of its _ scrollManger_ container
133
133
134
134
#### Offset scroll position
135
135
136
136
``` js
137
- windowScrollManager .scrollBy (number, options); // will offset scroll position, can be negative
137
+ windowScrollManager .scrollBy (number, options) // will offset scroll position, can be negative
138
138
```
139
139
140
140
#### options
@@ -144,61 +144,63 @@ const optionsDefault = {
144
144
duration: 0 , // duration of the scroll
145
145
horizontal: false , // direction of the scroll animation
146
146
center: 0 , // this value is used only for scrollToElement
147
- };
147
+ }
148
148
```
149
149
150
150
#### examples
151
151
152
152
``` js
153
153
windowScrollManager .scroll .toPercent (50 , {
154
154
duration: 1000 , // will scroll to the middle of the page vertically
155
- });
155
+ })
156
156
157
157
windowScrollManager .scroll .toPosition (50 , {
158
158
horizontal: true , // will scroll instantly to the middle of the page horizontally in 1 sec
159
- });
159
+ })
160
160
161
161
windowScrollManager .scroll .toElement (htmlElement, {
162
162
horizontally: true ,
163
163
duration: 1500 ,
164
164
center: 50 , // will scroll to the element and center it at 50% in the view (or its container)
165
- });
165
+ })
166
166
```
167
167
168
168
### Stop animations
169
169
170
170
``` js
171
- windowScrollManager .stopAllAnimations (); // this will stop all animations in windowScrollManager
171
+ windowScrollManager .stopAllAnimations () // this will stop all animations in windowScrollManager
172
172
173
173
// to stop specific animations, you'll have to capture them first:
174
- const animation = windowScrollManager .scroll .toPercent (50 , { duration: 1000 });
174
+ const animation = windowScrollManager .scroll .toPercent (50 , { duration: 1000 })
175
175
176
- animation .stop ();
176
+ animation .stop ()
177
177
```
178
178
179
179
### Change animation function
180
180
181
181
``` js
182
- windowScrollManager .easing = some_easing_function; // will change default easing function for next created animations
182
+ windowScrollManager .easing = some_easing_function // will change default easing function for next created animations
183
183
184
184
// or just for one animation:
185
- windowScrollManager .scroll .toPercent (50 , { duration: 1000 }).easing = some_easing_function;
185
+ windowScrollManager .scroll .toPercent (50 , {
186
+ duration: 1000 ,
187
+ }).easing = some_easing_function
186
188
```
187
189
188
190
### onScroll events
189
191
190
192
``` js
191
- const scrollManager = new Scroll (some_element);
193
+ const scrollManager = new Scroll (some_element)
192
194
193
- scrollManager .onScroll = () => console .log (" scroll ocurred in scrollManager container" );
194
- scrollManager .onUtilityScroll = () => console .log (" this scroll utility did scrolled" );
195
- scrollManager .onUserScroll = () => console .log (" this scroll utility did not scrolled!" );
195
+ scrollManager .onScroll = () => console .log (" scroll ocurred in scrollManager container" )
196
+ scrollManager .onUtilityScroll = () => console .log (" this scroll utility did scrolled" )
197
+ scrollManager .onUserScroll = () => console .log (" this scroll utility did not scrolled!" )
196
198
```
197
199
198
200
for example, if you wish to stop animation on user scroll you could do:
199
201
200
202
``` js
201
- scrollManager .onUserScroll = () => scrollManager .stopAllAnimations ();
203
+ scrollManager .onUserScroll = () => scrollManager .stopAllAnimations ()
202
204
```
203
205
204
206
### Stack animations and high precision
@@ -210,16 +212,16 @@ That is the best thing of scroll-utility. It is design to work with multiple ani
210
212
For example:
211
213
212
214
``` js
213
- scrollManager .scrollBy (500 , { duration: 1000 });
214
- scrollManager .scrollBy (34 , { duration: 775 });
215
+ scrollManager .scrollBy (500 , { duration: 1000 })
216
+ scrollManager .scrollBy (34 , { duration: 775 })
215
217
```
216
218
217
219
1 second from it started to move, it will have been offset its position for 534px
218
220
219
221
``` js
220
- scrollManager .scroll .toPosition (0 );
221
- scrollManager .scroll .toPercent (50 , { duration: 500 });
222
- scrollManager .scroll .toPercent (50 , { duration: 1000 });
222
+ scrollManager .scroll .toPosition (0 )
223
+ scrollManager .scroll .toPercent (50 , { duration: 500 })
224
+ scrollManager .scroll .toPercent (50 , { duration: 1000 })
223
225
```
224
226
225
227
in this example in 1 second to scroll bar will be at the end, due that it created 2 scroll animations that were to scroll 50% of the page (from 0 to 50), so 50% + 50% = 100%
0 commit comments