|
26 | 26 |
|
27 | 27 | 'use strict'; |
28 | 28 |
|
29 | | - /* |
30 | | - * Variables |
31 | | - */ |
| 29 | + // |
| 30 | + // Variables |
| 31 | + // |
32 | 32 |
|
33 | 33 | var exports = {}; // Object for public APIs |
34 | 34 | var supports = !!document.querySelector && !!root.addEventListener; // Feature test |
|
44 | 44 | }; |
45 | 45 |
|
46 | 46 |
|
47 | | - /* |
48 | | - * Methods |
49 | | - */ |
| 47 | + // |
| 48 | + // Methods |
| 49 | + // |
50 | 50 |
|
51 | | - // Merge defaults with user options |
52 | | - // @private |
53 | | - // @param {Object} defaults Default settings |
54 | | - // @param {Object} options User options |
55 | | - // @returns {Object} Merged values of defaults and options |
| 51 | + /** |
| 52 | + * Merge defaults with user options |
| 53 | + * @private |
| 54 | + * @param {Object} defaults Default settings |
| 55 | + * @param {Object} options User options |
| 56 | + * @returns {Object} Merged values of defaults and options |
| 57 | + */ |
56 | 58 | var extend = function ( defaults, options ) { |
57 | 59 | for ( var key in options ) { |
58 | 60 | if (Object.prototype.hasOwnProperty.call(options, key)) { |
|
62 | 64 | return defaults; |
63 | 65 | }; |
64 | 66 |
|
65 | | - // A simple forEach() implementation for Arrays, Objects and NodeLists |
66 | | - // @private |
67 | | - // @param {Array|Object|NodeList} collection Collection of items to iterate |
68 | | - // @param {Function} callback Callback function for each iteration |
69 | | - // @param {Array|Object|NodeList} scope Object/NodeList/Array that forEach is iterating over (aka `this`) |
| 67 | + /** |
| 68 | + * A simple forEach() implementation for Arrays, Objects and NodeLists |
| 69 | + * @private |
| 70 | + * @param {Array|Object|NodeList} collection Collection of items to iterate |
| 71 | + * @param {Function} callback Callback function for each iteration |
| 72 | + * @param {Array|Object|NodeList} scope Object/NodeList/Array that forEach is iterating over (aka `this`) |
| 73 | + */ |
70 | 74 | var forEach = function (collection, callback, scope) { |
71 | 75 | if (Object.prototype.toString.call(collection) === '[object Object]') { |
72 | 76 | for (var prop in collection) { |
|
81 | 85 | } |
82 | 86 | }; |
83 | 87 |
|
84 | | - // Calculate the easing pattern |
85 | | - // @private |
86 | | - // @param {String} type Easing pattern |
87 | | - // @param {Number} time Time animation should take to complete |
88 | | - // @returns {Number} |
| 88 | + /** |
| 89 | + * Calculate the easing pattern |
| 90 | + * @private |
| 91 | + * @param {String} type Easing pattern |
| 92 | + * @param {Number} time Time animation should take to complete |
| 93 | + * @returns {Number} |
| 94 | + */ |
89 | 95 | var easingPattern = function ( type, time ) { |
90 | 96 | var pattern; |
91 | 97 | if ( type === 'easeInQuad' ) pattern = time * time; // accelerating from zero velocity |
|
103 | 109 | return pattern || time; // no easing, no acceleration |
104 | 110 | }; |
105 | 111 |
|
106 | | - // Calculate how far to scroll |
107 | | - // @private |
108 | | - // @param {Element} anchor The anchor element to scroll to |
109 | | - // @param {Number} headerHeight Height of a fixed header, if any |
110 | | - // @param {Number} offset Number of pixels by which to offset scroll |
111 | | - // @returns {Number} |
| 112 | + /** |
| 113 | + * Calculate how far to scroll |
| 114 | + * @private |
| 115 | + * @param {Element} anchor The anchor element to scroll to |
| 116 | + * @param {Number} headerHeight Height of a fixed header, if any |
| 117 | + * @param {Number} offset Number of pixels by which to offset scroll |
| 118 | + * @returns {Number} |
| 119 | + */ |
112 | 120 | var getEndLocation = function ( anchor, headerHeight, offset ) { |
113 | 121 | var location = 0; |
114 | 122 | if (anchor.offsetParent) { |
|
121 | 129 | return location >= 0 ? location : 0; |
122 | 130 | }; |
123 | 131 |
|
124 | | - // Determine the document's height |
125 | | - // @private |
126 | | - // @returns {Number} |
| 132 | + /** |
| 133 | + * Determine the document's height |
| 134 | + * @private |
| 135 | + * @returns {Number} |
| 136 | + */ |
127 | 137 | var getDocumentHeight = function () { |
128 | 138 | return Math.max( |
129 | 139 | document.body.scrollHeight, document.documentElement.scrollHeight, |
|
132 | 142 | ); |
133 | 143 | }; |
134 | 144 |
|
135 | | - // Remove whitespace from a string |
136 | | - // @private |
137 | | - // @param {String} string |
138 | | - // @returns {String} |
| 145 | + /** |
| 146 | + * Remove whitespace from a string |
| 147 | + * @private |
| 148 | + * @param {String} string |
| 149 | + * @returns {String} |
| 150 | + */ |
139 | 151 | var trim = function ( string ) { |
140 | 152 | return string.replace(/^\s+|\s+$/g, ''); |
141 | 153 | }; |
142 | 154 |
|
143 | | - // Convert data-options attribute into an object of key/value pairs |
144 | | - // @private |
145 | | - // @param {String} options Link-specific options as a data attribute string |
146 | | - // @returns {Object} |
| 155 | + /** |
| 156 | + * Convert data-options attribute into an object of key/value pairs |
| 157 | + * @private |
| 158 | + * @param {String} options Link-specific options as a data attribute string |
| 159 | + * @returns {Object} |
| 160 | + */ |
147 | 161 | var getDataOptions = function ( options ) { |
148 | 162 | var settings = {}; |
149 | 163 | // Create a key/value pair for each setting |
|
160 | 174 | return settings; |
161 | 175 | }; |
162 | 176 |
|
163 | | - // Update the URL |
164 | | - // @private |
165 | | - // @param {Element} anchor The element to scroll to |
166 | | - // @param {Boolean} url Whether or not to update the URL history |
| 177 | + /** |
| 178 | + * Update the URL |
| 179 | + * @private |
| 180 | + * @param {Element} anchor The element to scroll to |
| 181 | + * @param {Boolean} url Whether or not to update the URL history |
| 182 | + */ |
167 | 183 | var updateUrl = function ( anchor, url ) { |
168 | 184 | if ( history.pushState && (url || url === 'true') ) { |
169 | 185 | history.pushState( { |
|
172 | 188 | } |
173 | 189 | }; |
174 | 190 |
|
175 | | - // Start/stop the scrolling animation |
176 | | - // @public |
177 | | - // @param {Element} toggle The element that toggled the scroll event |
178 | | - // @param {Element} anchor The element to scroll to |
179 | | - // @param {Object} settings |
180 | | - // @param {Event} event |
| 191 | + /** |
| 192 | + * Start/stop the scrolling animation |
| 193 | + * @public |
| 194 | + * @param {Element} toggle The element that toggled the scroll event |
| 195 | + * @param {Element} anchor The element to scroll to |
| 196 | + * @param {Object} settings |
| 197 | + * @param {Event} event |
| 198 | + */ |
181 | 199 | exports.animateScroll = function ( toggle, anchor, settings, event ) { |
182 | 200 |
|
183 | 201 | // Options and overrides |
|
207 | 225 | // Update URL |
208 | 226 | updateUrl(anchor, updateURL); |
209 | 227 |
|
210 | | - // Stop the scroll animation when it reaches its target (or the bottom/top of page) |
211 | | - // @private |
212 | | - // @param {Number} position Current position on the page |
213 | | - // @param {Number} endLocation Scroll to location |
214 | | - // @param {Number} animationInterval How much to scroll on this loop |
| 228 | + /** |
| 229 | + * Stop the scroll animation when it reaches its target (or the bottom/top of page) |
| 230 | + * @private |
| 231 | + * @param {Number} position Current position on the page |
| 232 | + * @param {Number} endLocation Scroll to location |
| 233 | + * @param {Number} animationInterval How much to scroll on this loop |
| 234 | + */ |
215 | 235 | var stopAnimateScroll = function (position, endLocation, animationInterval) { |
216 | 236 | var currentLocation = root.pageYOffset; |
217 | 237 | if ( position == endLocation || currentLocation == endLocation || ( (root.innerHeight + currentLocation) >= documentHeight ) ) { |
|
220 | 240 | } |
221 | 241 | }; |
222 | 242 |
|
223 | | - // Loop scrolling animation |
224 | | - // @private |
| 243 | + /** |
| 244 | + * Loop scrolling animation |
| 245 | + * @private |
| 246 | + */ |
225 | 247 | var loopAnimateScroll = function () { |
226 | 248 | timeLapsed += 16; |
227 | 249 | percentage = ( timeLapsed / speed ); |
|
231 | 253 | stopAnimateScroll(position, endLocation, animationInterval); |
232 | 254 | }; |
233 | 255 |
|
234 | | - // Set interval timer |
235 | | - // @private |
| 256 | + /** |
| 257 | + * Set interval timer |
| 258 | + * @private |
| 259 | + */ |
236 | 260 | var startAnimateScroll = function () { |
237 | 261 | settings.callbackBefore( toggle, anchor ); // Run callbacks before animating scroll |
238 | 262 | animationInterval = setInterval(loopAnimateScroll, 16); |
239 | 263 | }; |
240 | 264 |
|
241 | | - // Reset position to fix weird iOS bug |
242 | | - // @link https://github.com/cferdinandi/smooth-scroll/issues/45 |
| 265 | + /** |
| 266 | + * Reset position to fix weird iOS bug |
| 267 | + * @link https://github.com/cferdinandi/smooth-scroll/issues/45 |
| 268 | + */ |
243 | 269 | if ( root.pageYOffset === 0 ) { |
244 | 270 | root.scrollTo( 0, 0 ); |
245 | 271 | } |
|
249 | 275 |
|
250 | 276 | }; |
251 | 277 |
|
252 | | - // Initialize Smooth Scroll |
253 | | - // @public |
254 | | - // @param {Object} options User settings |
| 278 | + /** |
| 279 | + * Initialize Smooth Scroll |
| 280 | + * @public |
| 281 | + * @param {Object} options User settings |
| 282 | + */ |
255 | 283 | exports.init = function ( options ) { |
256 | 284 |
|
257 | 285 | // feature test |
|
269 | 297 | }; |
270 | 298 |
|
271 | 299 |
|
272 | | - /* |
273 | | - * Public APIs |
274 | | - */ |
| 300 | + // |
| 301 | + // Public APIs |
| 302 | + // |
275 | 303 |
|
276 | 304 | return exports; |
277 | 305 |
|
|
0 commit comments