kamrify commited on
Commit
b1c5f8c
·
1 Parent(s): 6b49118

Add trigger for when element has highlighted

Browse files
assets/scripts/src/element.js CHANGED
@@ -54,4 +54,9 @@ export default class Element {
54
 
55
  return position;
56
  }
 
 
 
 
 
57
  }
 
54
 
55
  return position;
56
  }
57
+
58
+ onHighlighted() {
59
+ console.log('on highlighted');
60
+ console.log(this.getScreenCoordinates());
61
+ }
62
  }
assets/scripts/src/overlay.js CHANGED
@@ -104,9 +104,9 @@ export default class Overlay {
104
  // Add the overlay on top of the whole body
105
  this.addCloak();
106
 
107
- if (canHighlight) {
108
- const isFadingIn = this.overlayAlpha < 0.1;
109
 
 
110
  if (isFadingIn) {
111
  // Ignore the animation, just highlight the item at its current position
112
  this.highlightedPosition = this.positionToHighlight;
@@ -151,12 +151,14 @@ export default class Overlay {
151
  }
152
 
153
  // Stage a new animation frame only if the position has not been reached
154
- // of the alpha has not yet fully reached fully required opacity
155
- if (
156
- !this.positionToHighlight.equals(this.highlightedPosition) ||
157
- this.overlayAlpha.toFixed(2) !== this.opacity.toFixed(2)
158
- ) {
159
  this.redrawAnimation = this.window.requestAnimationFrame(this.draw);
 
 
 
160
  }
161
  } else if (this.overlay.parentNode) {
162
  // Otherwise if the overlay is there, remove it
@@ -164,6 +166,11 @@ export default class Overlay {
164
  }
165
  }
166
 
 
 
 
 
 
167
  /**
168
  * Removes the cloak from the given position
169
  * i.e. cuts the chunk of layout which is over the element
 
104
  // Add the overlay on top of the whole body
105
  this.addCloak();
106
 
107
+ const isFadingIn = this.overlayAlpha < 0.1;
 
108
 
109
+ if (canHighlight) {
110
  if (isFadingIn) {
111
  // Ignore the animation, just highlight the item at its current position
112
  this.highlightedPosition = this.positionToHighlight;
 
151
  }
152
 
153
  // Stage a new animation frame only if the position has not been reached
154
+ // or the alpha has not yet fully reached fully required opacity
155
+ if (!this.hasPositionHighlighted()) {
156
+ this.redrawAnimation = this.window.requestAnimationFrame(this.draw);
157
+ } else if (!this.animate && isFadingIn) {
 
158
  this.redrawAnimation = this.window.requestAnimationFrame(this.draw);
159
+ } else {
160
+ // Element has been highlighted
161
+ this.highlightedElement.onHighlighted();
162
  }
163
  } else if (this.overlay.parentNode) {
164
  // Otherwise if the overlay is there, remove it
 
166
  }
167
  }
168
 
169
+ hasPositionHighlighted() {
170
+ return this.positionToHighlight.equals(this.highlightedPosition) &&
171
+ this.overlayAlpha > (this.opacity - 0.05);
172
+ }
173
+
174
  /**
175
  * Removes the cloak from the given position
176
  * i.e. cuts the chunk of layout which is over the element
assets/scripts/src/position.js CHANGED
@@ -35,9 +35,9 @@ export default class Position {
35
  * @returns {boolean}
36
  */
37
  equals(position) {
38
- return this.left.toFixed(3) === position.left.toFixed(3) &&
39
- this.right.toFixed(3) === position.right.toFixed(3) &&
40
- this.top.toFixed(3) === position.top.toFixed(3) &&
41
- this.bottom.toFixed(3) === position.bottom.toFixed(3);
42
  }
43
  }
 
35
  * @returns {boolean}
36
  */
37
  equals(position) {
38
+ return Math.round(this.left) === Math.round(position.left) &&
39
+ Math.round(this.right) === Math.round(position.right) &&
40
+ Math.round(this.top) === Math.round(position.top) &&
41
+ Math.round(this.bottom) === Math.round(position.bottom);
42
  }
43
  }
index.html CHANGED
@@ -44,7 +44,10 @@
44
 
45
  <script src="./assets/scripts/dist/sholo.js"></script>
46
  <script>
47
- const sholo = new Sholo();
 
 
 
48
  sholo.highlight('.section__header');
49
  </script>
50
  </body>
 
44
 
45
  <script src="./assets/scripts/dist/sholo.js"></script>
46
  <script>
47
+ const sholo = new Sholo({
48
+ animate: false,
49
+ opacity: 0.8
50
+ });
51
  sholo.highlight('.section__header');
52
  </script>
53
  </body>