Skip to content
This repository was archived by the owner on Apr 28, 2020. It is now read-only.

Commit 300b684

Browse files
author
Chris Ferdinandi
committed
Merge branch 'updated-URL'
2 parents 3dc805c + 3d3d9a2 commit 300b684

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ A simple script to animate scrolling to anchor links. Easing support contributed
55
Getting started with Smooth Scroll is really easy. [View the online tutorial](http://cferdinandi.github.com/smooth-scroll/) or dig through the `index.html` file.
66

77
## Changelog
8+
* v2.10 (December 31, 2013)
9+
* [Added URL history support.](https://github.com/cferdinandi/smooth-scroll/pull/17)
810
* v2.9 (December 9, 2013)
911
* [Added fixed for infinite loop when scrolling up.](https://github.com/cferdinandi/smooth-scroll/issues/13)
1012
* v2.8 (December 3, 2013)

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ <h1 style="text-align: center; font-size: 3em;">Smooth Scroll</h1>
6060
.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.
6161
</p>
6262

63-
<p><a class="scroll" data-speed="400" data-easing="easeOutQuad" href="#top">Back to the top</a></p>
63+
<p><a class="scroll" data-speed="400" data-easing="easeOutQuad" data-url="false" href="#top">Back to the top</a></p>
6464

6565
</section>
6666

smooth-scroll.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* =============================================================
22
3-
Smooth Scroll 2.9
3+
Smooth Scroll 2.10
44
Animate scrolling to anchor links, by Chris Ferdinandi.
55
http://gomakethings.com
66
@@ -10,6 +10,9 @@
1010
Easing functions forked from Gaëtan Renaudeau.
1111
https://gist.github.com/gre/1650294
1212
13+
URL history support contributed by Robert Pate.
14+
https://github.com/robertpateii
15+
1316
Free to use under the MIT License.
1417
http://gomakethings.com/mit/
1518
@@ -23,7 +26,7 @@
2326
if ( 'querySelector' in document && 'addEventListener' in window && Array.prototype.forEach ) {
2427

2528
// Function to animate the scroll
26-
var smoothScroll = function (anchor, duration, easing) {
29+
var smoothScroll = function (anchor, duration, easing, url) {
2730

2831
// Functions to control easing
2932
var easingPattern = function (type, time) {
@@ -42,6 +45,13 @@
4245
return time; // no easing, no acceleration
4346
};
4447

48+
// Function to update URL
49+
var updateURL = function (url, anchor) {
50+
if ( url === 'true' && history.pushState ) {
51+
history.pushState(null, null, '#' + anchor.id);
52+
}
53+
};
54+
4555
// Calculate how far and how fast to scroll
4656
// http://www.quirksmode.org/blog/archives/2008/01/using_the_assig.html
4757
var startLocation = window.pageYOffset;
@@ -77,6 +87,7 @@
7787
var travelled = window.pageYOffset;
7888
if ( (travelled >= (endLocation(anchor) - increments)) || ((window.innerHeight + travelled) >= document.body.offsetHeight) ) {
7989
clearInterval(runAnimation);
90+
updateURL(url, anchor);
8091
}
8192
};
8293
} else { // If scrolling up
@@ -85,6 +96,7 @@
8596
var travelled = window.pageYOffset;
8697
if ( travelled <= endLocation(anchor) || travelled <= 0 ) {
8798
clearInterval(runAnimation);
99+
updateURL(url, anchor);
88100
}
89101
};
90102
}
@@ -108,12 +120,13 @@
108120
var dataID = toggle.getAttribute('href');
109121
var dataTarget = document.querySelector(dataID);
110122
var dataSpeed = toggle.getAttribute('data-speed');
111-
var dataEasing = toggle.getAttribute('data-easing'); // WL: Added easing attribute support.
123+
var dataEasing = toggle.getAttribute('data-easing');
124+
var dataURL = toggle.getAttribute('data-url');
112125

113126
// If the anchor exists
114127
if (dataTarget) {
115128
// Scroll to the anchor
116-
smoothScroll(dataTarget, dataSpeed || 500, dataEasing || 'easeInOutCubic');
129+
smoothScroll(dataTarget, dataSpeed || 500, dataEasing || 'easeInOutCubic', dataURL || 'true');
117130
}
118131

119132
}, false);

0 commit comments

Comments
 (0)