mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
45 lines
1.0 KiB
JavaScript
45 lines
1.0 KiB
JavaScript
/**
|
|
* v-clickoutside
|
|
* @desc 点击元素外面才会触发的事件
|
|
* @example
|
|
* ```vue
|
|
* <div v-clickoutside="handleClose">
|
|
* ```
|
|
*/
|
|
import Vue from 'vue';
|
|
const isServer = Vue.prototype.$isServer;
|
|
const clickoutsideContext = '@@clickoutsideContext';
|
|
|
|
export default {
|
|
bind(el, binding, vnode) {
|
|
const documentHandler = function(e) {
|
|
if (vnode.context && !el.contains(e.target)) {
|
|
vnode.context[el[clickoutsideContext].methodName]();
|
|
}
|
|
};
|
|
el[clickoutsideContext] = {
|
|
documentHandler,
|
|
methodName: binding.expression,
|
|
arg: binding.arg || 'click'
|
|
};
|
|
!isServer && document.addEventListener(el[clickoutsideContext].arg, documentHandler);
|
|
},
|
|
|
|
update(el, binding) {
|
|
el[clickoutsideContext].methodName = binding.expression;
|
|
},
|
|
|
|
unbind(el) {
|
|
!isServer && document.removeEventListener(
|
|
el[clickoutsideContext].arg,
|
|
el[clickoutsideContext].documentHandler);
|
|
},
|
|
|
|
install(Vue) {
|
|
Vue.directive('clickoutside', {
|
|
bind: this.bind,
|
|
unbind: this.unbind
|
|
});
|
|
}
|
|
};
|