Remove highlight on click outside of the highlighted area
Browse files- assets/scripts/src/overlay.js +8 -0
- assets/scripts/src/sholo.js +14 -0
- index.html +2 -1
assets/scripts/src/overlay.js
CHANGED
|
@@ -79,6 +79,14 @@ export default class Overlay {
|
|
| 79 |
this.draw();
|
| 80 |
}
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
/**
|
| 83 |
* Removes the overlay and cancel any listeners
|
| 84 |
*/
|
|
|
|
| 79 |
this.draw();
|
| 80 |
}
|
| 81 |
|
| 82 |
+
/**
|
| 83 |
+
* Returns the currently selected element
|
| 84 |
+
* @returns {null|*}
|
| 85 |
+
*/
|
| 86 |
+
getHighlightedElement() {
|
| 87 |
+
return this.highlightedElement;
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
/**
|
| 91 |
* Removes the overlay and cancel any listeners
|
| 92 |
*/
|
assets/scripts/src/sholo.js
CHANGED
|
@@ -24,6 +24,7 @@ export default class Sholo {
|
|
| 24 |
this.onScroll = this.onScroll.bind(this);
|
| 25 |
this.onResize = this.onResize.bind(this);
|
| 26 |
this.onKeyUp = this.onKeyUp.bind(this);
|
|
|
|
| 27 |
|
| 28 |
// Event bindings
|
| 29 |
this.bind();
|
|
@@ -38,6 +39,19 @@ export default class Sholo {
|
|
| 38 |
this.document.addEventListener('DOMMouseScroll', this.onScroll, false);
|
| 39 |
this.window.addEventListener('resize', this.onResize, false);
|
| 40 |
this.window.addEventListener('keyup', this.onKeyUp, false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
}
|
| 42 |
|
| 43 |
/**
|
|
|
|
| 24 |
this.onScroll = this.onScroll.bind(this);
|
| 25 |
this.onResize = this.onResize.bind(this);
|
| 26 |
this.onKeyUp = this.onKeyUp.bind(this);
|
| 27 |
+
this.onMouseUp = this.onMouseUp.bind(this);
|
| 28 |
|
| 29 |
// Event bindings
|
| 30 |
this.bind();
|
|
|
|
| 39 |
this.document.addEventListener('DOMMouseScroll', this.onScroll, false);
|
| 40 |
this.window.addEventListener('resize', this.onResize, false);
|
| 41 |
this.window.addEventListener('keyup', this.onKeyUp, false);
|
| 42 |
+
this.window.addEventListener('mouseup', this.onMouseUp, false);
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
onMouseUp(e) {
|
| 46 |
+
const highlightedElement = this.overlay.getHighlightedElement();
|
| 47 |
+
if (!highlightedElement || !highlightedElement.node) {
|
| 48 |
+
return;
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
// Remove the overlay If clicked outside the highlighted element
|
| 52 |
+
if (!highlightedElement.node.contains(e.target)) {
|
| 53 |
+
this.overlay.clear();
|
| 54 |
+
}
|
| 55 |
}
|
| 56 |
|
| 57 |
/**
|
index.html
CHANGED
|
@@ -46,7 +46,8 @@
|
|
| 46 |
<script>
|
| 47 |
const sholo = new Sholo({
|
| 48 |
animate: true,
|
| 49 |
-
opacity: 0.8
|
|
|
|
| 50 |
});
|
| 51 |
sholo.highlight('.section__header');
|
| 52 |
</script>
|
|
|
|
| 46 |
<script>
|
| 47 |
const sholo = new Sholo({
|
| 48 |
animate: true,
|
| 49 |
+
opacity: 0.8,
|
| 50 |
+
padding: 0
|
| 51 |
});
|
| 52 |
sholo.highlight('.section__header');
|
| 53 |
</script>
|