@contentpilot/sticky-navigation 1.5.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 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 syntaxis of `querySelector`
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 syntaxis of `querySelectorAll`
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 syntaxis of `querySelectorAll`
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 = {
@@ -52,7 +52,7 @@ const options = {
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,16 @@ 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
+
63
73
  #### inactiveUpperZone: Number
64
74
 
65
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`.
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.0
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
- _addScrollBehaivor(options.offsetAnchor);
76
+ _addScrollBehavior(options.offsetAnchor, options.offsetElement);
77
77
 
78
78
  const elements = _getElements(options.identifiers);
79
79
 
@@ -109,6 +109,9 @@ function _applyRemainingDefaultOptions(opts) {
109
109
  opts.offsetAnchor = opts.hasOwnProperty('offsetAnchor')
110
110
  ? opts.offsetAnchor
111
111
  : 0;
112
+ opts.offsetElement = opts.hasOwnProperty('offsetElement')
113
+ ? opts.offsetElement
114
+ : '';
112
115
  opts.inactiveUpperZone = opts.hasOwnProperty('inactiveUpperZone')
113
116
  ? opts.inactiveUpperZone
114
117
  : 0;
@@ -182,12 +185,33 @@ function _getElements(input) {
182
185
  return elements;
183
186
  }
184
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
+
185
208
  /**
186
209
  * Add smooth scroll behavior to the proper anchor.
187
210
  *
188
- * @param {number} offset - Offset distance when scrolling.
211
+ * @param {number} offsetFixed - Offset distance when scrolling.
212
+ * @param {string} offsetElement - The element to calculate the offset from.
189
213
  */
190
- function _addScrollBehaivor(offset) {
214
+ function _addScrollBehavior(offsetFixed, offsetElement) {
191
215
  document.querySelectorAll('a[href^="#"]').forEach(function (anchor) {
192
216
  anchor.addEventListener('click', function (e) {
193
217
  e.preventDefault();
@@ -198,6 +222,9 @@ function _addScrollBehaivor(offset) {
198
222
  // Position of element relative to screen + Window scrolling position.
199
223
  const top = rect.top + window.scrollY;
200
224
 
225
+ // Fixed offset or offset element height including margins.
226
+ const offset = offsetElement ? _getElementFullHeight(offsetElement) : offsetFixed;
227
+
201
228
  window.location.hash = id;
202
229
 
203
230
  window.scroll({
package/package-lock.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentpilot/sticky-navigation",
3
- "version": "1.5.0",
3
+ "version": "1.6.0",
4
4
  "lockfileVersion": 1,
5
5
  "requires": true,
6
6
  "dependencies": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentpilot/sticky-navigation",
3
- "version": "1.5.0",
3
+ "version": "1.6.0",
4
4
  "description": "Add identifiers and activate the anchors that have these identifier and are intercepted with the scroll.",
5
5
  "main": "index.js",
6
6
  "publishConfig": {