@cocreate/scroll 1.3.5 → 1.3.6

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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [1.3.6](https://github.com/CoCreate-app/CoCreate-scroll/compare/v1.3.5...v1.3.6) (2022-06-22)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * compare datetime to break infinite loop on scroll up and scroll down ([c86b317](https://github.com/CoCreate-app/CoCreate-scroll/commit/c86b3170a9335acf895006de54d10b2cc3fc2ab0))
7
+
1
8
  ## [1.3.5](https://github.com/CoCreate-app/CoCreate-scroll/compare/v1.3.4...v1.3.5) (2022-06-19)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/scroll",
3
- "version": "1.3.5",
3
+ "version": "1.3.6",
4
4
  "description": "A simple scroll component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "scroll",
package/src/index.js CHANGED
@@ -3,6 +3,9 @@ import observer from '@cocreate/observer';
3
3
  const CoCreateScroll = {
4
4
  delta: 3,
5
5
  observer: null,
6
+ timer: null,
7
+ firedEvents: new Map(),
8
+
6
9
  init: function() {
7
10
  let elements = document.querySelectorAll(`[scroll]`);
8
11
  this.__initIntersectionObserver();
@@ -20,8 +23,9 @@ const CoCreateScroll = {
20
23
  const downSize = this.__getSize(element.getAttribute('scroll-down'));
21
24
  const attrName = element.getAttribute('scroll-attribute') || 'class';
22
25
  const targetSelector = element.getAttribute('scroll-target');
26
+ const scrollSelector = element.getAttribute('scroll-element');
23
27
  const intersectValue = element.getAttribute('scroll-intersect');
24
-
28
+
25
29
  let values = element.getAttribute('scroll') || "";
26
30
  values = values.split(",").map(x => x.trim());
27
31
 
@@ -45,34 +49,55 @@ const CoCreateScroll = {
45
49
  el.scrollStatus = { currentPos: 0 };
46
50
  });
47
51
 
48
- this.__runScrollEvent(element, scrollInfo);
52
+ // this.__runScrollEvent(element, scrollInfo);
49
53
 
50
- let timer = null;
51
- window.addEventListener('scroll', function(event) {
52
- if (!element.scrollStatus) return;
53
- if (Math.abs(window.scrollY - element.scrollStatus.currentPos) <= self.delta) {
54
- return;
55
- }
56
-
57
- if (timer != null) {
58
- clearTimeout(timer);
54
+ let scrollableElements;
55
+ if (scrollSelector)
56
+ scrollableElements = document.querySelectorAll(scrollSelector);
57
+
58
+ if (scrollableElements) {
59
+ for (let scrollableEl of scrollableElements) {
60
+ scrollableEl.addEventListener('scroll', function(event) {
61
+ self._scrollEvent(elements, element, scrollInfo, scrollableEl)
62
+ });
59
63
  }
64
+ } else{
65
+ // this.WindowInit = true;
66
+ window.addEventListener('scroll', function(event) {
67
+ self._scrollEvent(elements, element, scrollInfo)
68
+ });
69
+ }
60
70
 
61
- elements.forEach((el) => {
62
- self.__runScrollEvent(el, scrollInfo);
63
- self.__setScrolling(el, scrollInfo, false);
64
- });
65
-
66
- timer = setTimeout(function() {
67
- elements.forEach((el) => {
68
- self.__setScrolling(el, scrollInfo, true);
69
- });
70
- }, 500);
71
- });
72
71
  if (intersectValue && window.IntersectionObserver && this.observer) {
73
72
  this.observer.observe(element);
74
73
  }
75
74
  },
75
+
76
+ _scrollEvent: function(elements, element, scrollInfo, scrollableEl) {
77
+ const self = this;
78
+ if (!element.scrollStatus) return;
79
+ let scrollEl = scrollableEl || window;
80
+ if (Math.abs(scrollEl.scrollTop || scrollEl.scrollY - element.scrollStatus.currentPos) <= self.delta) {
81
+ return;
82
+ }
83
+
84
+ let timer = null;
85
+ if (timer != null) {
86
+ clearTimeout(timer);
87
+ }
88
+
89
+ elements.forEach((el) => {
90
+ self.__setScrolling(el, scrollInfo, false);
91
+ self.__runScrollEvent(el, scrollInfo, scrollableEl);
92
+
93
+ });
94
+
95
+ timer = setTimeout(function() {
96
+ elements.forEach((el) => {
97
+ self.__setScrolling(el, scrollInfo, true);
98
+ });
99
+ }, 500);
100
+ },
76
101
 
77
102
  __initIntersectionObserver: function() {
78
103
  const self = this;
@@ -107,21 +132,34 @@ const CoCreateScroll = {
107
132
  }
108
133
  },
109
134
 
110
- __runScrollEvent: function(element, info) {
135
+ __runScrollEvent: function(element, info, scrollableEl) {
111
136
  if (!element.scrollStatus) return
112
137
  const currentPos = element.scrollStatus.currentPos;
113
- const scrollY = window.scrollY;
138
+ let scrollY, scrollHeight, innerHeight;
139
+ if (scrollableEl){
140
+ scrollY = scrollableEl.scrollTop;
141
+ scrollHeight = scrollableEl.scrollHeight
142
+ innerHeight = scrollableEl.clientHeight
143
+ } else {
144
+ scrollY = window.scrollY;
145
+ scrollHeight = document.body.scrollHeight
146
+ innerHeight = window.innerHeight
147
+ }
114
148
  const { upSize, downSize, attrName, values, scrollTop, scrollBottom, scrollLimbo } = info;
115
149
 
116
- if (upSize < (currentPos - scrollY)) {
117
- this.__addAttributeValue(element, attrName, values[0]);
118
- this.__removeAttrbuteValue(element, attrName, values[1]);
119
- }
120
- else if (downSize < (scrollY - currentPos)) {
121
- this.__removeAttrbuteValue(element, attrName, values[0]);
122
- this.__addAttributeValue(element, attrName, values[1]);
123
- }
150
+ let newTime = new Date().getTime()
151
+ if (!info.datetime || (newTime - info.datetime) > 200 ) {
152
+ info['datetime'] = newTime
124
153
 
154
+ if (upSize <= (currentPos - scrollY)) {
155
+ this.__addAttributeValue(element, attrName, values[0]);
156
+ this.__removeAttrbuteValue(element, attrName, values[1]);
157
+ }
158
+ else if (downSize <= (scrollY - currentPos)) {
159
+ this.__removeAttrbuteValue(element, attrName, values[0]);
160
+ this.__addAttributeValue(element, attrName, values[1]);
161
+ }
162
+ }
125
163
  //. scroll top case
126
164
  if (scrollY <= this.delta) {
127
165
  this.__removeAttrbuteValue(element, attrName, values[0]);
@@ -134,7 +172,8 @@ const CoCreateScroll = {
134
172
  }
135
173
 
136
174
  //. scroll bottom case
137
- if ((window.innerHeight + scrollY) >= document.body.scrollHeight) {
175
+ // if ((window.innerHeight + scrollY) >= document.body.scrollHeight) {
176
+ if ((innerHeight + scrollY) >= scrollHeight) {
138
177
  // this.__removeAttrbuteValue(element, attrName, values[0]);
139
178
  // this.__removeAttrbuteValue(element, attrName, values[1]);
140
179
 
@@ -144,7 +183,8 @@ const CoCreateScroll = {
144
183
  this.__removeAttrbuteValue(element, attrName, scrollBottom);
145
184
  }
146
185
 
147
- if (scrollY != 0 && (scrollY + window.innerHeight) != document.body.scrollHeight){
186
+ // if (scrollY != 0 && (scrollY + window.innerHeight) != document.body.scrollHeight){
187
+ if (scrollY != 0 && (scrollY + innerHeight) != scrollHeight){
148
188
  this.__addAttributeValue(element, attrName, scrollLimbo);
149
189
  }
150
190
  else {
@@ -197,8 +237,6 @@ const CoCreateScroll = {
197
237
  return size;
198
238
  }
199
239
 
200
-
201
-
202
240
  };
203
241
 
204
242
  observer.init({
@@ -213,4 +251,4 @@ observer.init({
213
251
 
214
252
  CoCreateScroll.init();
215
253
 
216
- export default CoCreateScroll;
254
+ export default CoCreateScroll;