Skip to content

Commit 7de1eb0

Browse files
author
Chris Ferdinandi
committed
Merge branch 'different-docs'
2 parents 010ad46 + 5474ba6 commit 7de1eb0

File tree

1 file changed

+93
-65
lines changed

1 file changed

+93
-65
lines changed

smooth-scroll.js

Lines changed: 93 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626

2727
'use strict';
2828

29-
/*
30-
* Variables
31-
*/
29+
//
30+
// Variables
31+
//
3232

3333
var exports = {}; // Object for public APIs
3434
var supports = !!document.querySelector && !!root.addEventListener; // Feature test
@@ -44,15 +44,17 @@
4444
};
4545

4646

47-
/*
48-
* Methods
49-
*/
47+
//
48+
// Methods
49+
//
5050

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+
*/
5658
var extend = function ( defaults, options ) {
5759
for ( var key in options ) {
5860
if (Object.prototype.hasOwnProperty.call(options, key)) {
@@ -62,11 +64,13 @@
6264
return defaults;
6365
};
6466

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+
*/
7074
var forEach = function (collection, callback, scope) {
7175
if (Object.prototype.toString.call(collection) === '[object Object]') {
7276
for (var prop in collection) {
@@ -81,11 +85,13 @@
8185
}
8286
};
8387

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+
*/
8995
var easingPattern = function ( type, time ) {
9096
var pattern;
9197
if ( type === 'easeInQuad' ) pattern = time * time; // accelerating from zero velocity
@@ -103,12 +109,14 @@
103109
return pattern || time; // no easing, no acceleration
104110
};
105111

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+
*/
112120
var getEndLocation = function ( anchor, headerHeight, offset ) {
113121
var location = 0;
114122
if (anchor.offsetParent) {
@@ -121,9 +129,11 @@
121129
return location >= 0 ? location : 0;
122130
};
123131

124-
// Determine the document's height
125-
// @private
126-
// @returns {Number}
132+
/**
133+
* Determine the document's height
134+
* @private
135+
* @returns {Number}
136+
*/
127137
var getDocumentHeight = function () {
128138
return Math.max(
129139
document.body.scrollHeight, document.documentElement.scrollHeight,
@@ -132,18 +142,22 @@
132142
);
133143
};
134144

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+
*/
139151
var trim = function ( string ) {
140152
return string.replace(/^\s+|\s+$/g, '');
141153
};
142154

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+
*/
147161
var getDataOptions = function ( options ) {
148162
var settings = {};
149163
// Create a key/value pair for each setting
@@ -160,10 +174,12 @@
160174
return settings;
161175
};
162176

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+
*/
167183
var updateUrl = function ( anchor, url ) {
168184
if ( history.pushState && (url || url === 'true') ) {
169185
history.pushState( {
@@ -172,12 +188,14 @@
172188
}
173189
};
174190

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+
*/
181199
exports.animateScroll = function ( toggle, anchor, settings, event ) {
182200

183201
// Options and overrides
@@ -207,11 +225,13 @@
207225
// Update URL
208226
updateUrl(anchor, updateURL);
209227

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+
*/
215235
var stopAnimateScroll = function (position, endLocation, animationInterval) {
216236
var currentLocation = root.pageYOffset;
217237
if ( position == endLocation || currentLocation == endLocation || ( (root.innerHeight + currentLocation) >= documentHeight ) ) {
@@ -220,8 +240,10 @@
220240
}
221241
};
222242

223-
// Loop scrolling animation
224-
// @private
243+
/**
244+
* Loop scrolling animation
245+
* @private
246+
*/
225247
var loopAnimateScroll = function () {
226248
timeLapsed += 16;
227249
percentage = ( timeLapsed / speed );
@@ -231,15 +253,19 @@
231253
stopAnimateScroll(position, endLocation, animationInterval);
232254
};
233255

234-
// Set interval timer
235-
// @private
256+
/**
257+
* Set interval timer
258+
* @private
259+
*/
236260
var startAnimateScroll = function () {
237261
settings.callbackBefore( toggle, anchor ); // Run callbacks before animating scroll
238262
animationInterval = setInterval(loopAnimateScroll, 16);
239263
};
240264

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+
*/
243269
if ( root.pageYOffset === 0 ) {
244270
root.scrollTo( 0, 0 );
245271
}
@@ -249,9 +275,11 @@
249275

250276
};
251277

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+
*/
255283
exports.init = function ( options ) {
256284

257285
// feature test
@@ -269,9 +297,9 @@
269297
};
270298

271299

272-
/*
273-
* Public APIs
274-
*/
300+
//
301+
// Public APIs
302+
//
275303

276304
return exports;
277305

0 commit comments

Comments
 (0)