Skip to content

Commit 5159523

Browse files
committed
Changed the way speed and duration are calculated
1 parent 53e8937 commit 5159523

17 files changed

+513
-312
lines changed

.github/contributing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Report bugs, ask questions, and request features using [GitHub Issues](https://g
1111
- Clearly demonstrate the bug or issue.
1212
- Include the bare minimum HTML, CSS, and JavaScript required to demonstrate the bug.
1313
- A link to your production site is **not** a reduced test case.
14-
- You can create one by [forking this JSFiddle](https://jsfiddle.net/cferdinandi/87v3pqp0/).
14+
- You can create one by [forking this CodePen](https://codepen.io/cferdinandi/pen/RqGLpz).
1515
3. The browser and OS that you're using.
1616

1717
Duplicates and issues without a reduced test case may be closed without comment.

.github/issue_template.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<!-- 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. -->
22

3-
**Test case:** https://jsfiddle.net/cferdinandi/87v3pqp0/
3+
**Test case:** https://codepen.io/cferdinandi/pen/RqGLpz

README.md

Lines changed: 101 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Smooth Scroll [![Build Status](https://travis-ci.org/cferdinandi/smooth-scroll.svg)](https://travis-ci.org/cferdinandi/smooth-scroll)
22
A lightweight script to animate scrolling to anchor links. Smooth Scroll works great with [Gumshoe](https://github.com/cferdinandi/gumshoe).
33

4-
[Demo](http://cferdinandi.github.io/smooth-scroll/)
4+
[View the Demo](https://codepen.io/cferdinandi/pen/wQzrdM)
55

66

77
<hr>
@@ -11,7 +11,6 @@ A lightweight script to animate scrolling to anchor links. Smooth Scroll works g
1111
<hr>
1212

1313

14-
1514
## Getting Started
1615

1716
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
7473
***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'`.*
7574

7675

77-
## ES6 Modules
7876

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.
8082

8183
```js
82-
import('/path/to/smooth-scroll.polyfills.min.js')
83-
.then(function () {
84-
var scroll = new SmoothScroll('a[href*="#"]');
85-
});
84+
var scroll = new SmoothScroll('a[href*="#"]', {
85+
speed: 300
86+
});
8687
```
8788

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`.
8990

91+
```js
92+
// All animations will take exactly 500ms
93+
var scroll = new SmoothScroll('a[href*="#"]', {
94+
speed: 500,
95+
speedAsDuration: true
96+
});
97+
```
9098

9199

92-
## Working with the Source Files
100+
## Easing Options
93101

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)
95103

96-
### Dependencies
97-
Make sure these are installed first.
104+
**Linear**
105+
*Moves at the same speed from start to finish.*
98106

99-
* [Node.js](http://nodejs.org)
100-
* [Gulp](http://gulpjs.com) `sudo npm install -g gulp`
107+
- `Linear`
101108

102-
### Quick Start
103109

104-
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 = new SmoothScroll('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+
```
109153

110154

111155

@@ -125,8 +169,11 @@ var scroll = new SmoothScroll('a[href*="#"]', {
125169
header: null, // Selector for fixed headers (must be a valid CSS selector)
126170
topOnEmptyHash: true, // Scroll to the top of the page for links with href="#"
127171

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
130177
clip: true, // If true, adjust scroll distance to prevent abrupt stops near the bottom of the page
131178
offset: function (anchor, toggle) {
132179

@@ -141,6 +188,8 @@ var scroll = new SmoothScroll('a[href*="#"]', {
141188
}
142189

143190
},
191+
192+
// Easing
144193
easing: 'easeInOutCubic', // Easing pattern to use
145194
customEasing: function (time) {
146195

@@ -164,44 +213,6 @@ var scroll = new SmoothScroll('a[href*="#"]', {
164213
});
165214
```
166215

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/).
204-
205216
### Custom Events
206217

207218
Smooth Scroll emits three custom events:
@@ -233,7 +244,7 @@ document.addEventListener('scrollStop', logScrollEvent, false);
233244
document.addEventListener('scrollCancel', logScrollEvent, false);
234245
```
235246

236-
### Use Smooth Scroll events in your own scripts
247+
### Methods
237248

238249
You can also call Smooth Scroll's methods in your own scripts.
239250

@@ -321,34 +332,31 @@ If you have multiple fixed headers, pass in the last one in the markup.
321332
```
322333

323334

324-
## Migrating to Smooth Scroll 14 from Older Versions
325-
326-
### New Features
327335

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
334337

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.
336339

337-
- Callback methods have been removed in favor of events.
340+
### Dependencies
341+
Make sure these are installed first.
338342

343+
* [Node.js](http://nodejs.org)
344+
* [Gulp](http://gulpjs.com) `sudo npm install -g gulp`
339345

346+
### Quick Start
340347

341-
## Browser Compatibility
348+
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/).
342353

343-
Smooth Scroll works in all modern browsers, and IE 9 and above.
344354

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.
346355

347-
### Polyfills
348356

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
350358

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`.
352360

353361

354362

@@ -370,6 +378,20 @@ Most browsers instantly jump you to the anchor location when you load a page. Yo
370378

371379

372380

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+
373395
## License
374396

375397
The code is available under the [MIT License](LICENSE.md).

0 commit comments

Comments
 (0)