@iris.interactive/handcook 2.7.4 → 2.7.5

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
@@ -1,5 +1,5 @@
1
1
  # Welcome to HandCook 👨‍🍳
2
- ![Version](https://img.shields.io/badge/version-2.7.4-blue.svg?cacheSeconds=2592000)
2
+ ![Version](https://img.shields.io/badge/version-2.7.5-blue.svg?cacheSeconds=2592000)
3
3
  ![Prerequisite](https://img.shields.io/badge/node-%3E%3D%2012.14.0-blue.svg)
4
4
  [![License: UNLICENSED](https://img.shields.io/badge/License-UNLICENSED-yellow.svg)](#)
5
5
  [![Twitter: captain\_iris](https://img.shields.io/twitter/follow/captain\_iris.svg?style=social)](https://twitter.com/captain\_iris)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iris.interactive/handcook",
3
- "version": "2.7.4",
3
+ "version": "2.7.5",
4
4
  "description": "The web cooking by IRIS Interactive",
5
5
  "main": "./public/scripts/index.js",
6
6
  "scripts": {
@@ -8,11 +8,12 @@ export class HcScrollspy {
8
8
  constructor(elements = ElementEnum.scrollspy) {
9
9
  document.querySelectorAll(elements).forEach(element => {
10
10
  const navElement = document.querySelector(element.dataset.hcScrollspy);
11
- this.activeItem(navElement, ElementEnum.scrollSmooth);
11
+ const shift = element.getAttribute('data-hc-scrollspy-shift') !== null ? parseInt(element.getAttribute('data-hc-scrollspy-shift')) : 0;
12
+ this.activeItem(navElement, ElementEnum.scrollSmooth, shift);
12
13
  });
13
14
  }
14
15
 
15
- getOffsetTop(navElement, childElement) {
16
+ getPartsConfig(navElement, childElement) {
16
17
  let offsetTop = [];
17
18
  navElement.querySelectorAll(childElement).forEach(item => {
18
19
  const part = item.getAttribute('href') !== null ? {
@@ -25,7 +26,8 @@ export class HcScrollspy {
25
26
  attribute: SmoothScrollEnum.attrHref
26
27
  };
27
28
  if (document.querySelector(part.id) !== null) {
28
- part.value = document.querySelector(part.id).offsetTop;
29
+ // part.value = document.querySelector(part.id).offsetTop;
30
+ part.value = this.getOffset(document.querySelector(part.id)).top;
29
31
  offsetTop = [
30
32
  ...offsetTop,
31
33
  part
@@ -35,13 +37,13 @@ export class HcScrollspy {
35
37
  return offsetTop;
36
38
  }
37
39
 
38
- activeItem(navElement, childElement) {
40
+ activeItem(navElement, childElement, shift = 0) {
39
41
  let item = navElement.querySelector(`${childElement}:first-child`);
40
42
  window.addEventListener('scroll', () => {
41
- const offsetTop = this.getOffsetTop(navElement, childElement);
43
+ const partsConfig = this.getPartsConfig(navElement, childElement);
42
44
  const top = window.scrollY;
43
- offsetTop.forEach(offsetTop => {
44
- if (top > (offsetTop.value - 150)) {
45
+ partsConfig.forEach(offsetTop => {
46
+ if (top > (offsetTop.value - (shift + 50))) {
45
47
  item = navElement.querySelector('[' + offsetTop.attribute + '="' + offsetTop.id + '"]');
46
48
  }
47
49
  })
@@ -71,6 +73,17 @@ export class HcScrollspy {
71
73
  }
72
74
  });
73
75
  }
76
+
77
+ getOffset( element ) {
78
+ let x = 0;
79
+ let y = 0;
80
+ while( element && !isNaN( element.offsetLeft ) && !isNaN( element.offsetTop ) ) {
81
+ x += element.offsetLeft - element.scrollLeft;
82
+ y += element.offsetTop - element.scrollTop;
83
+ element = element.offsetParent;
84
+ }
85
+ return { top: y, left: x };
86
+ }
74
87
  }
75
88
 
76
89
  const hc_scrollspy = function (trigger) {