|
1 | 1 | /* =============================================================
|
2 | 2 |
|
3 |
| - Smooth Scroll 2.9 |
| 3 | + Smooth Scroll 2.10 |
4 | 4 | Animate scrolling to anchor links, by Chris Ferdinandi.
|
5 | 5 | http://gomakethings.com
|
6 | 6 |
|
|
10 | 10 | Easing functions forked from Gaëtan Renaudeau.
|
11 | 11 | https://gist.github.com/gre/1650294
|
12 | 12 |
|
| 13 | + URL history support contributed by Robert Pate. |
| 14 | + https://github.com/robertpateii |
| 15 | +
|
13 | 16 | Free to use under the MIT License.
|
14 | 17 | http://gomakethings.com/mit/
|
15 | 18 |
|
|
23 | 26 | if ( 'querySelector' in document && 'addEventListener' in window && Array.prototype.forEach ) {
|
24 | 27 |
|
25 | 28 | // Function to animate the scroll
|
26 |
| - var smoothScroll = function (anchor, duration, easing) { |
| 29 | + var smoothScroll = function (anchor, duration, easing, url) { |
27 | 30 |
|
28 | 31 | // Functions to control easing
|
29 | 32 | var easingPattern = function (type, time) {
|
|
42 | 45 | return time; // no easing, no acceleration
|
43 | 46 | };
|
44 | 47 |
|
| 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 | + |
45 | 55 | // Calculate how far and how fast to scroll
|
46 | 56 | // http://www.quirksmode.org/blog/archives/2008/01/using_the_assig.html
|
47 | 57 | var startLocation = window.pageYOffset;
|
|
77 | 87 | var travelled = window.pageYOffset;
|
78 | 88 | if ( (travelled >= (endLocation(anchor) - increments)) || ((window.innerHeight + travelled) >= document.body.offsetHeight) ) {
|
79 | 89 | clearInterval(runAnimation);
|
| 90 | + updateURL(url, anchor); |
80 | 91 | }
|
81 | 92 | };
|
82 | 93 | } else { // If scrolling up
|
|
85 | 96 | var travelled = window.pageYOffset;
|
86 | 97 | if ( travelled <= endLocation(anchor) || travelled <= 0 ) {
|
87 | 98 | clearInterval(runAnimation);
|
| 99 | + updateURL(url, anchor); |
88 | 100 | }
|
89 | 101 | };
|
90 | 102 | }
|
|
108 | 120 | var dataID = toggle.getAttribute('href');
|
109 | 121 | var dataTarget = document.querySelector(dataID);
|
110 | 122 | 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'); |
112 | 125 |
|
113 | 126 | // If the anchor exists
|
114 | 127 | if (dataTarget) {
|
115 | 128 | // Scroll to the anchor
|
116 |
| - smoothScroll(dataTarget, dataSpeed || 500, dataEasing || 'easeInOutCubic'); |
| 129 | + smoothScroll(dataTarget, dataSpeed || 500, dataEasing || 'easeInOutCubic', dataURL || 'true'); |
117 | 130 | }
|
118 | 131 |
|
119 | 132 | }, false);
|
|
0 commit comments