Remove highlight on click outside of the highlighted area

This commit is contained in:
Kamran Ahmed
2018-03-06 00:21:06 +01:00
parent 0c119f7ea1
commit 22fca24daf
3 changed files with 24 additions and 1 deletions
+8
View File
@@ -79,6 +79,14 @@ export default class Overlay {
this.draw();
}
/**
* Returns the currently selected element
* @returns {null|*}
*/
getHighlightedElement() {
return this.highlightedElement;
}
/**
* Removes the overlay and cancel any listeners
*/
+14
View File
@@ -24,6 +24,7 @@ export default class Sholo {
this.onScroll = this.onScroll.bind(this);
this.onResize = this.onResize.bind(this);
this.onKeyUp = this.onKeyUp.bind(this);
this.onMouseUp = this.onMouseUp.bind(this);
// Event bindings
this.bind();
@@ -38,6 +39,19 @@ export default class Sholo {
this.document.addEventListener('DOMMouseScroll', this.onScroll, false);
this.window.addEventListener('resize', this.onResize, false);
this.window.addEventListener('keyup', this.onKeyUp, false);
this.window.addEventListener('mouseup', this.onMouseUp, false);
}
onMouseUp(e) {
const highlightedElement = this.overlay.getHighlightedElement();
if (!highlightedElement || !highlightedElement.node) {
return;
}
// Remove the overlay If clicked outside the highlighted element
if (!highlightedElement.node.contains(e.target)) {
this.overlay.clear();
}
}
/**
+2 -1
View File
@@ -46,7 +46,8 @@
<script>
const sholo = new Sholo({
animate: true,
opacity: 0.8
opacity: 0.8,
padding: 0
});
sholo.highlight('.section__header');
</script>