v-clickoutsides

//点击目标元素外侧触发特定事件 使用 v-clickoutsides="clickHandler"
import Vue from 'vue'

Vue.directive('clickoutsides', {
  bind: function(el, binding, vnode) {
    function documentHandler(e) {
      if (el.contains(e.target)) {
        return false
      }
      if (binding.expression) {
        binding.value(e)
      }
    }
    el.__vueClickOutsie__ = documentHandler
    document.addEventListener('click', documentHandler)
  },
  unbind: function(el, binding) {
    document.removeEventListener('click', el.__vueClickOutsie__)
    delete el.__vueClickOutsie__
  }
})

posted @ 2020-12-01 19:24  Samsara315  阅读(202)  评论(0编辑  收藏  举报