Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ColtHands
Created August 16, 2020 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ColtHands/ff90fcec252b2a606bf3405a7eae11f8 to your computer and use it in GitHub Desktop.
Save ColtHands/ff90fcec252b2a606bf3405a7eae11f8 to your computer and use it in GitHub Desktop.
Vue click outside element directive
Vue.directive('click-outside-element', {
bind(el, bind, vn) {
el.cO = event => {
if (!(el == event.target || el.contains(event.target))) {
if(vn.context[bind.expression]) {
vn.context[bind.expression](event)
}
}
}
document.body.addEventListener('click', el.cO)
},
unbind(el) {
document.body.removeEventListener('click', el.cO)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment