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
<!-- Thanks for submitting an issue! All bug reports and problem issues require a **reduced test case**. Create one forking the JSFiddle linked below. See the guidelines link above for more details. -->
1
+
<!-- Thanks for submitting an issue! All bug reports and problem issues require a **reduced test case**. Create one forking the CodePen linked below. See the guidelines link above for more details. -->
[View the Demo](https://codepen.io/cferdinandi/pen/wQzrdM)
5
5
6
6
7
7
<hr>
@@ -11,7 +11,6 @@ A lightweight script to animate scrolling to anchor links. Smooth Scroll works g
11
11
<hr>
12
12
13
13
14
-
15
14
## Getting Started
16
15
17
16
Compiled and production-ready code can be found in the `dist` directory. The `src` directory contains development code.
@@ -74,38 +73,83 @@ In the footer of your page, after the content, initialize Smooth Scroll by passi
74
73
***Note:****The `a[href*="#"]` selector will apply Smooth Scroll to all anchor links. You can selectively target links using any other selector(s) you'd like. Smooth Scroll accepts multiple selectors as a comma separated list. Example: `'.js-scroll, [data-scroll], #some-link'`.*
75
74
76
75
77
-
## ES6 Modules
78
76
79
-
Smooth Scroll does not have a default export, but does support CommonJS and can be used with native ES6 module imports.
77
+
## Scroll Speed
78
+
79
+
Smooth Scroll allows you to adjust the speed of your animations with the `speed` option.
80
+
81
+
This a number representing the amount of time in milliseconds that it should take to scroll 1000px. Scroll distances shorter than that will take less time, and scroll distances longer than that will take more time. The default is 300ms.
80
82
81
83
```js
82
-
import('/path/to/smooth-scroll.polyfills.min.js')
83
-
.then(function () {
84
-
var scroll =newSmoothScroll('a[href*="#"]');
85
-
});
84
+
var scroll =newSmoothScroll('a[href*="#"]', {
85
+
speed:300
86
+
});
86
87
```
87
88
88
-
It uses a UMD pattern, and should also work in most major module bundlers and package managers.
89
+
If you want all of your animations to take exactly the same amount of time (the value you set for `speed`), set the `speedAsDuration` option to `true`.
89
90
91
+
```js
92
+
// All animations will take exactly 500ms
93
+
var scroll =newSmoothScroll('a[href*="#"]', {
94
+
speed:500,
95
+
speedAsDuration:true
96
+
});
97
+
```
90
98
91
99
92
-
## Working with the Source Files
100
+
## Easing Options
93
101
94
-
If you would prefer, you can work with the development code in the `src` directory using the included [Gulp build system](http://gulpjs.com/). This compiles, lints, and minifies code.
102
+
Smooth Scroll comes with about a dozen common easing patterns. [Here's a demo of the different patterns.](https://codepen.io/cferdinandi/pen/jQMGaB)
1. In bash/terminal/command line, `cd` into your project directory.
105
-
2. Run `npm install` to install required files.
106
-
3. When it's done installing, run one of the task runners to get going:
107
-
* `gulp` manually compiles files.
108
-
* `gulp watch` automatically compiles files when changes are made and applies changes using [LiveReload](http://livereload.com/).
110
+
**Ease-In**
111
+
*Gradually increases in speed.*
112
+
113
+
-`easeInQuad`
114
+
-`easeInCubic`
115
+
-`easeInQuart`
116
+
-`easeInQuint`
117
+
118
+
119
+
**Ease-In-Out**
120
+
*Gradually increases in speed, peaks, and then gradually slows down.*
121
+
122
+
-`easeInOutQuad`
123
+
-`easeInOutCubic`
124
+
-`easeInOutQuart`
125
+
-`easeInOutQuint`
126
+
127
+
128
+
**Ease-Out**
129
+
*Gradually decreases in speed.*
130
+
131
+
-`easeOutQuad`
132
+
-`easeOutCubic`
133
+
-`easeOutQuart`
134
+
-`easeOutQuint`
135
+
136
+
137
+
You can also pass in your own custom easing pattern [using the `customEasing` option](#global-settings).
138
+
139
+
```js
140
+
var scroll =newSmoothScroll('a[href*="#"]', {
141
+
// Function. Custom easing pattern
142
+
// If this is set to anything other than null, will override the easing option above
143
+
customEasing:function (time) {
144
+
145
+
// return <your formulate with time as a multiplier>
146
+
147
+
// Example: easeInOut Quad
148
+
return time <0.5?2* time * time :-1+ (4-2* time) * time;
149
+
150
+
}
151
+
});
152
+
```
109
153
110
154
111
155
@@ -125,8 +169,11 @@ var scroll = new SmoothScroll('a[href*="#"]', {
125
169
header:null, // Selector for fixed headers (must be a valid CSS selector)
126
170
topOnEmptyHash:true, // Scroll to the top of the page for links with href="#"
127
171
128
-
// Speed & Easing
129
-
speed:500, // Integer. How fast to complete the scroll in milliseconds
172
+
// Speed & Duration
173
+
speed:500, // Integer. Amount of time in milliseconds it should take to scroll 1000px
174
+
speedAsDuration:false, // If true, use speed as the total duration of the scroll animation
175
+
durationMax:null, // Integer. The maximum amount of time the scroll animation should take
176
+
durationMin:null, // Integer. The minimum amount of time the scroll animation should take
130
177
clip:true, // If true, adjust scroll distance to prevent abrupt stops near the bottom of the page
131
178
offset:function (anchor, toggle) {
132
179
@@ -141,6 +188,8 @@ var scroll = new SmoothScroll('a[href*="#"]', {
141
188
}
142
189
143
190
},
191
+
192
+
// Easing
144
193
easing:'easeInOutCubic', // Easing pattern to use
145
194
customEasing:function (time) {
146
195
@@ -164,44 +213,6 @@ var scroll = new SmoothScroll('a[href*="#"]', {
164
213
});
165
214
```
166
215
167
-
#### Easing Options
168
-
169
-
Some common easing patterns are included by default, but you can also pass in your own custom easing pattern using the `customEasing` option noted above.
170
-
171
-
**Linear**
172
-
*Moves at the same speed from start to finish.*
173
-
174
-
*`Linear`
175
-
176
-
177
-
**Ease-In**
178
-
*Gradually increases in speed.*
179
-
180
-
*`easeInQuad`
181
-
*`easeInCubic`
182
-
*`easeInQuart`
183
-
*`easeInQuint`
184
-
185
-
186
-
**Ease-In-Out**
187
-
*Gradually increases in speed, peaks, and then gradually slows down.*
188
-
189
-
*`easeInOutQuad`
190
-
*`easeInOutCubic`
191
-
*`easeInOutQuart`
192
-
*`easeInOutQuint`
193
-
194
-
195
-
**Ease-Out**
196
-
*Gradually decreases in speed.*
197
-
198
-
*`easeOutQuad`
199
-
*`easeOutCubic`
200
-
*`easeOutQuart`
201
-
*`easeOutQuint`
202
-
203
-
Learn more about the different easing patterns and what they do at [easings.net](http://easings.net/).
You can also call Smooth Scroll's methods in your own scripts.
239
250
@@ -321,34 +332,31 @@ If you have multiple fixed headers, pass in the last one in the markup.
321
332
```
322
333
323
334
324
-
## Migrating to Smooth Scroll 14 from Older Versions
325
-
326
-
### New Features
327
335
328
-
- You can now choose not to update the URL as a native feature. No more hacks using the API.
329
-
- You can choose not to scroll to top on `href="#"`, providing more flexibility for some CMS implementations.
330
-
- Custom events let you more easily hook into Smooth Scroll from other scripts.
331
-
- The feature test is scoped to a function, allowing for server-side implementations.
332
-
- The loss of styling with IDs experienced in earlier versions has been eliminated.
333
-
- The `anchor` and `toggle` elements are now passed into the `offset()` function, allowing for more customization [v14.1.0 and up].
336
+
## Working with the Source Files
334
337
335
-
### Breaking Changes
338
+
If you would prefer, you can work with the development code in the `src` directory using the included [Gulp build system](http://gulpjs.com/). This compiles, lints, and minifies code.
336
339
337
-
- Callback methods have been removed in favor of events.
1. In bash/terminal/command line, `cd` into your project directory.
349
+
2. Run `npm install` to install required files.
350
+
3. When it's done installing, run one of the task runners to get going:
351
+
* `gulp` manually compiles files.
352
+
* `gulp watch` automatically compiles files when changes are made and applies changes using [LiveReload](http://livereload.com/).
342
353
343
-
Smooth Scroll works in all modern browsers, and IE 9 and above.
344
354
345
-
Smooth Scroll is built with modern JavaScript APIs, and uses progressive enhancement. If the JavaScript file fails to load, or if your site is viewed on older and less capable browsers, anchor links will jump the way they normally would.
346
355
347
-
### Polyfills
348
356
349
-
Support back to IE9 requires polyfills for `closest()`, `requestAnimationFrame()`, and `CustomEvent()`. Without them, support starts with Edge.
357
+
## Migrating to Smooth Scroll 15 from Older Versions
350
358
351
-
Use the included polyfills version of Smooth Scroll, or include your own.
359
+
Scroll duration now varies based on distance traveled. If you want to maintain the old scroll animation duration behavior, set the `speedAsDuration` option to `true`.
352
360
353
361
354
362
@@ -370,6 +378,20 @@ Most browsers instantly jump you to the anchor location when you load a page. Yo
370
378
371
379
372
380
381
+
## Browser Compatibility
382
+
383
+
Smooth Scroll works in all modern browsers, and IE 9 and above.
384
+
385
+
Smooth Scroll is built with modern JavaScript APIs, and uses progressive enhancement. If the JavaScript file fails to load, or if your site is viewed on older and less capable browsers, anchor links will jump the way they normally would.
386
+
387
+
### Polyfills
388
+
389
+
Support back to IE9 requires polyfills for `closest()`, `requestAnimationFrame()`, and `CustomEvent()`. Without them, support starts with Edge.
390
+
391
+
Use the included polyfills version of Smooth Scroll, or include your own.
392
+
393
+
394
+
373
395
## License
374
396
375
397
The code is available under the [MIT License](LICENSE.md).
0 commit comments