@contentpilot/sticky-navigation 1.4.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -5
- package/index.js +77 -28
- package/package-lock.json +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -22,7 +22,7 @@ The options parameter is optional.
|
|
|
22
22
|
|
|
23
23
|
#### clause: String
|
|
24
24
|
|
|
25
|
-
Clause element selector. The script will not run if this element does not exist. Default is `.sticky-navigation`. Use
|
|
25
|
+
Clause element selector. The script will not run if this element does not exist. Default is `.sticky-navigation`. Use syntax of `querySelector`
|
|
26
26
|
|
|
27
27
|
```
|
|
28
28
|
const options = {
|
|
@@ -32,7 +32,7 @@ const options = {
|
|
|
32
32
|
|
|
33
33
|
#### identifiers: String
|
|
34
34
|
|
|
35
|
-
Selector of elements to which the ID attribute will be added. Default is `h2, h3, h4, h5, h6`. Use
|
|
35
|
+
Selector of elements to which the ID attribute will be added. Default is `h2, h3, h4, h5, h6`. Use syntax of `querySelectorAll`
|
|
36
36
|
|
|
37
37
|
```
|
|
38
38
|
const options = {
|
|
@@ -42,7 +42,7 @@ const options = {
|
|
|
42
42
|
|
|
43
43
|
#### interceptors: String
|
|
44
44
|
|
|
45
|
-
Selector of elements that when intercepted will add the active class in the anchors that have that destination. Default is `h2[id], h3[id], h4[id], h5[id], h6[id]`. Use
|
|
45
|
+
Selector of elements that when intercepted will add the active class in the anchors that have that destination. Default is `h2[id], h3[id], h4[id], h5[id], h6[id]`. Use syntax of `querySelectorAll`
|
|
46
46
|
|
|
47
47
|
```
|
|
48
48
|
const options = {
|
|
@@ -50,9 +50,9 @@ const options = {
|
|
|
50
50
|
};
|
|
51
51
|
```
|
|
52
52
|
|
|
53
|
-
####
|
|
53
|
+
#### offsetAnchor: Number
|
|
54
54
|
|
|
55
|
-
Offset distance when scrolling. Useful when using a fixed header. Default is `0`.
|
|
55
|
+
Offset distance when scrolling. Useful when using a fixed header. Default is `0`. Ignored if offsetElement was defined.
|
|
56
56
|
|
|
57
57
|
```
|
|
58
58
|
const options = {
|
|
@@ -60,6 +60,36 @@ const options = {
|
|
|
60
60
|
};
|
|
61
61
|
```
|
|
62
62
|
|
|
63
|
+
#### offsetElement: String
|
|
64
|
+
|
|
65
|
+
Element selector to calculate offset dynamically. Useful when the offset varies according to an element that is not always present. Default is `empty`.
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
const options = {
|
|
69
|
+
offsetElement: ''
|
|
70
|
+
};
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
#### inactiveUpperZone: Number
|
|
74
|
+
|
|
75
|
+
Distance in pixels from the top where items are not activated. Useful when you have a fixed header, you scrolls up and you don't want to activate the element while it is behind the header. Default is `0`.
|
|
76
|
+
|
|
77
|
+
```
|
|
78
|
+
const options = {
|
|
79
|
+
inactiveUpperZone: 170
|
|
80
|
+
};
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
#### interceptionPercentage: Number
|
|
84
|
+
|
|
85
|
+
Distance in percentage from the top for the items to activate. If for example you want the items to activate when they are in the middle of the screen, use 50. Default is `20`.
|
|
86
|
+
|
|
87
|
+
```
|
|
88
|
+
const options = {
|
|
89
|
+
interceptionPercentage: 50
|
|
90
|
+
};
|
|
91
|
+
```
|
|
92
|
+
|
|
63
93
|
### Publish to NPM
|
|
64
94
|
|
|
65
95
|
To update this repository on NPM using the GitHub action,
|
package/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Add identifiers and activate the anchors that have these identifier
|
|
3
3
|
* and are intercepted with the scroll. It needs the proper markup.
|
|
4
4
|
*
|
|
5
|
-
* @version 1.
|
|
5
|
+
* @version 1.6.0
|
|
6
6
|
* @package
|
|
7
7
|
* @author Content Pilot, Dallas, Texas
|
|
8
8
|
* @copyright Copyright © 2018 Content Pilot
|
|
@@ -73,7 +73,7 @@ function _callStack(options) {
|
|
|
73
73
|
options = _applyRemainingDefaultOptions(options);
|
|
74
74
|
|
|
75
75
|
if (null !== document.querySelector(options.clause)) {
|
|
76
|
-
|
|
76
|
+
_addScrollBehavior(options.offsetAnchor, options.offsetElement);
|
|
77
77
|
|
|
78
78
|
const elements = _getElements(options.identifiers);
|
|
79
79
|
|
|
@@ -82,7 +82,11 @@ function _callStack(options) {
|
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
_addIdentifiers(elements);
|
|
85
|
-
_addIntersectionObserver(
|
|
85
|
+
_addIntersectionObserver(
|
|
86
|
+
options.interceptors,
|
|
87
|
+
options.inactiveUpperZone,
|
|
88
|
+
options.interceptionPercentage
|
|
89
|
+
);
|
|
86
90
|
}
|
|
87
91
|
}
|
|
88
92
|
|
|
@@ -102,7 +106,18 @@ function _applyRemainingDefaultOptions(opts) {
|
|
|
102
106
|
opts.interceptors = opts.hasOwnProperty('interceptors')
|
|
103
107
|
? opts.interceptors
|
|
104
108
|
: 'h2[id], h3[id], h4[id], h5[id], h6[id]';
|
|
105
|
-
opts.
|
|
109
|
+
opts.offsetAnchor = opts.hasOwnProperty('offsetAnchor')
|
|
110
|
+
? opts.offsetAnchor
|
|
111
|
+
: 0;
|
|
112
|
+
opts.offsetElement = opts.hasOwnProperty('offsetElement')
|
|
113
|
+
? opts.offsetElement
|
|
114
|
+
: '';
|
|
115
|
+
opts.inactiveUpperZone = opts.hasOwnProperty('inactiveUpperZone')
|
|
116
|
+
? opts.inactiveUpperZone
|
|
117
|
+
: 0;
|
|
118
|
+
opts.interceptionPercentage = opts.hasOwnProperty('interceptionPercentage')
|
|
119
|
+
? 100 - opts.interceptionPercentage
|
|
120
|
+
: 80;
|
|
106
121
|
|
|
107
122
|
return opts;
|
|
108
123
|
}
|
|
@@ -170,12 +185,33 @@ function _getElements(input) {
|
|
|
170
185
|
return elements;
|
|
171
186
|
}
|
|
172
187
|
|
|
188
|
+
/**
|
|
189
|
+
* Get the element full height including margins.
|
|
190
|
+
*
|
|
191
|
+
* @param {string} selector - A CSS selector string.
|
|
192
|
+
* @returns {number} - The element height including margins.
|
|
193
|
+
*/
|
|
194
|
+
function _getElementFullHeight(selector) {
|
|
195
|
+
const element = document.querySelector(selector);
|
|
196
|
+
|
|
197
|
+
if (!element) {
|
|
198
|
+
return 0;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const height = element.offsetHeight;
|
|
202
|
+
const styles = window.getComputedStyle(element);
|
|
203
|
+
const margin = parseInt(styles.getPropertyValue('margin-top')) + parseInt(styles.getPropertyValue('margin-bottom'));
|
|
204
|
+
|
|
205
|
+
return (height + margin);
|
|
206
|
+
}
|
|
207
|
+
|
|
173
208
|
/**
|
|
174
209
|
* Add smooth scroll behavior to the proper anchor.
|
|
175
210
|
*
|
|
176
|
-
* @param {number}
|
|
211
|
+
* @param {number} offsetFixed - Offset distance when scrolling.
|
|
212
|
+
* @param {string} offsetElement - The element to calculate the offset from.
|
|
177
213
|
*/
|
|
178
|
-
function
|
|
214
|
+
function _addScrollBehavior(offsetFixed, offsetElement) {
|
|
179
215
|
document.querySelectorAll('a[href^="#"]').forEach(function (anchor) {
|
|
180
216
|
anchor.addEventListener('click', function (e) {
|
|
181
217
|
e.preventDefault();
|
|
@@ -186,6 +222,9 @@ function _addScrollBehaivor(offset) {
|
|
|
186
222
|
// Position of element relative to screen + Window scrolling position.
|
|
187
223
|
const top = rect.top + window.scrollY;
|
|
188
224
|
|
|
225
|
+
// Fixed offset or offset element height including margins.
|
|
226
|
+
const offset = offsetElement ? _getElementFullHeight(offsetElement) : offsetFixed;
|
|
227
|
+
|
|
189
228
|
window.location.hash = id;
|
|
190
229
|
|
|
191
230
|
window.scroll({
|
|
@@ -228,25 +267,35 @@ function _urlify(text) {
|
|
|
228
267
|
* Watch at the intersection with the elements and add or remove the active class.
|
|
229
268
|
*
|
|
230
269
|
* @param {string} observables - Observable elements selector.
|
|
270
|
+
* @param {string} inactiveUpperZone - Distance in pixels from the top where items are not activated.
|
|
271
|
+
* @param {string} interceptionPercentage - Distance in percentage from the top for the items to activate.
|
|
231
272
|
*/
|
|
232
|
-
function _addIntersectionObserver(
|
|
273
|
+
function _addIntersectionObserver(
|
|
274
|
+
observables,
|
|
275
|
+
inactiveUpperZone,
|
|
276
|
+
interceptionPercentage
|
|
277
|
+
) {
|
|
278
|
+
const rootMargin = `-${inactiveUpperZone}px 0px -${interceptionPercentage}% 0px`;
|
|
279
|
+
|
|
233
280
|
window.addEventListener('DOMContentLoaded', function () {
|
|
234
|
-
const observer = new IntersectionObserver(
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
if (
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
281
|
+
const observer = new IntersectionObserver(
|
|
282
|
+
function (entries) {
|
|
283
|
+
entries.forEach(function (entry) {
|
|
284
|
+
const id = entry.target.getAttribute('id');
|
|
285
|
+
const anchor = document.querySelector(
|
|
286
|
+
'nav li a[href="#' + id + '"]'
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
if (anchor) {
|
|
290
|
+
if (entry.isIntersecting) {
|
|
291
|
+
_removeActiveClass(anchor);
|
|
292
|
+
_addActiveClass(anchor);
|
|
293
|
+
}
|
|
246
294
|
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
295
|
+
});
|
|
296
|
+
},
|
|
297
|
+
{ rootMargin: rootMargin }
|
|
298
|
+
);
|
|
250
299
|
|
|
251
300
|
// Track all the elements defined as observables that have an `id` applied
|
|
252
301
|
document.querySelectorAll(observables).forEach(function (observable) {
|
|
@@ -261,12 +310,12 @@ function _addIntersectionObserver(observables) {
|
|
|
261
310
|
* @param {object} node - Current node.
|
|
262
311
|
*/
|
|
263
312
|
function _addActiveClass(node) {
|
|
313
|
+
if (node) {
|
|
314
|
+
const activeElements = node.parentElement.parentElement.querySelectorAll(
|
|
315
|
+
'.current-menu-item'
|
|
316
|
+
);
|
|
264
317
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
const activeElements = node.parentElement.parentElement.querySelectorAll('.current-menu-item');
|
|
268
|
-
|
|
269
|
-
activeElements.forEach(element => {
|
|
318
|
+
activeElements.forEach(function (element) {
|
|
270
319
|
element.classList.remove('current-menu-item');
|
|
271
320
|
});
|
|
272
321
|
|
|
@@ -280,7 +329,7 @@ function _addActiveClass(node) {
|
|
|
280
329
|
* @param {object} node - Current node.
|
|
281
330
|
*/
|
|
282
331
|
function _removeActiveClass(node) {
|
|
283
|
-
if(node
|
|
332
|
+
if (node) {
|
|
284
333
|
node.parentElement.classList.remove('current-menu-item');
|
|
285
334
|
}
|
|
286
335
|
}
|
package/package-lock.json
CHANGED
package/package.json
CHANGED