Handle scrolling and resizing
Browse files- assets/scripts/src/overlay.js +7 -1
- assets/scripts/src/sholo.js +26 -4
assets/scripts/src/overlay.js
CHANGED
|
@@ -161,13 +161,19 @@ export default class Overlay {
|
|
| 161 |
// cut out a chunk for the element to be visible out of it
|
| 162 |
this.overlay.width = width || this.window.innerWidth;
|
| 163 |
this.overlay.height = height || this.window.innerHeight;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
// If the highlighted element was there Cancel the
|
| 166 |
// existing animation frame if any and highlight it again
|
| 167 |
// as its position might have been changed
|
| 168 |
if (this.highlightedElement) {
|
| 169 |
this.window.cancelAnimationFrame(this.redrawAnimation);
|
| 170 |
-
this.highlight(this.highlightedElement);
|
| 171 |
}
|
| 172 |
}
|
| 173 |
}
|
|
|
|
| 161 |
// cut out a chunk for the element to be visible out of it
|
| 162 |
this.overlay.width = width || this.window.innerWidth;
|
| 163 |
this.overlay.height = height || this.window.innerHeight;
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
// Refreshes the overlay i.e. sets the size according to current window size
|
| 167 |
+
// And moves the highlight around if necessary
|
| 168 |
+
refresh(animate = true) {
|
| 169 |
+
this.setSize();
|
| 170 |
|
| 171 |
// If the highlighted element was there Cancel the
|
| 172 |
// existing animation frame if any and highlight it again
|
| 173 |
// as its position might have been changed
|
| 174 |
if (this.highlightedElement) {
|
| 175 |
this.window.cancelAnimationFrame(this.redrawAnimation);
|
| 176 |
+
this.highlight(this.highlightedElement, animate);
|
| 177 |
}
|
| 178 |
}
|
| 179 |
}
|
assets/scripts/src/sholo.js
CHANGED
|
@@ -7,10 +7,32 @@ import './polyfill';
|
|
| 7 |
*/
|
| 8 |
export default class Sholo {
|
| 9 |
constructor({ opacity = 0.75, padding = 5 } = {}) {
|
| 10 |
-
this.overlay = new Overlay({
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
}
|
| 15 |
|
| 16 |
highlight(selector) {
|
|
|
|
| 7 |
*/
|
| 8 |
export default class Sholo {
|
| 9 |
constructor({ opacity = 0.75, padding = 5 } = {}) {
|
| 10 |
+
this.overlay = new Overlay({ opacity, padding });
|
| 11 |
+
this.document = document;
|
| 12 |
+
this.window = window;
|
| 13 |
+
|
| 14 |
+
this.onScroll = this.onScroll.bind(this);
|
| 15 |
+
this.onResize = this.onResize.bind(this);
|
| 16 |
+
|
| 17 |
+
// Event bindings
|
| 18 |
+
this.bind();
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
bind() {
|
| 22 |
+
// @todo: add throttling in all the listeners
|
| 23 |
+
this.document.addEventListener('scroll', this.onScroll, false);
|
| 24 |
+
this.document.addEventListener('DOMMouseScroll', this.onScroll, false);
|
| 25 |
+
this.window.addEventListener('resize', this.onResize, false);
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
onScroll() {
|
| 29 |
+
// Refresh without animation on scroll
|
| 30 |
+
this.overlay.refresh(false);
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
onResize() {
|
| 34 |
+
// Refresh with animation
|
| 35 |
+
this.overlay.refresh(true);
|
| 36 |
}
|
| 37 |
|
| 38 |
highlight(selector) {
|