Commit 08dc000c by 胡悦

添加事件外点击事件

1 parent 4e2a7c1e
...@@ -3,10 +3,10 @@ ...@@ -3,10 +3,10 @@
* @Author: huyue * @Author: huyue
* @Date: 2023-07-05 15:11:53 * @Date: 2023-07-05 15:11:53
* @LastEditors: huyue * @LastEditors: huyue
* @LastEditTime: 2023-07-11 11:20:38 * @LastEditTime: 2023-07-12 15:28:36
--> -->
<template> <template>
<div v-show="isShow" class="select-box" :style="{'width': boxWidth}"> <div v-click-outside="handleClickOutside" ref="box" @mouseleave="handleMouseLeave" v-show="isShow" class="select-box" :style="{'width': boxWidth}">
<slot name="search"></slot> <slot name="search"></slot>
<div class="year-item" :class="(isObject ? item[valueName] : item) == value ? 'active' : ''" v-for="(item,index) in dataList" :key="index" @click="select(item)">{{isObject ? item[labelName] : item}}</div> <div class="year-item" :class="(isObject ? item[valueName] : item) == value ? 'active' : ''" v-for="(item,index) in dataList" :key="index" @click="select(item)">{{isObject ? item[labelName] : item}}</div>
</div> </div>
...@@ -41,6 +41,23 @@ export default { ...@@ -41,6 +41,23 @@ export default {
type: String type: String
} }
}, },
directives: {
'click-outside': {
bind: function(el, binding, vnode) {
el.clickOutsideEvent = function(event) {
// 判断点击事件是否在绑定元素的内部
if (!(el == event.target || el.contains(event.target))) {
// 调用绑定的回调函数
vnode.context[binding.expression](event)
}
}
document.body.addEventListener('click', el.clickOutsideEvent)
},
unbind: function(el) {
document.body.removeEventListener('click', el.clickOutsideEvent)
}
}
},
data() { data() {
return { return {
value: '' value: ''
...@@ -55,6 +72,13 @@ export default { ...@@ -55,6 +72,13 @@ export default {
const { valueName, isObject } = this const { valueName, isObject } = this
this.value = isObject ? item[valueName] : item this.value = isObject ? item[valueName] : item
this.$emit('selectVal', item) this.$emit('selectVal', item)
},
handleMouseLeave() {
// 在此处处理鼠标移出事件的逻辑
},
handleClickOutside() {
this.$emit('clickOutside')
// 在此处处理鼠标点击事件的逻辑
} }
} }
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @Author: 前端 qianduan01@didihu.com.cn * @Author: 前端 qianduan01@didihu.com.cn
* @Date: 2022-10-25 10:12:26 * @Date: 2022-10-25 10:12:26
* @LastEditors: huyue * @LastEditors: huyue
* @LastEditTime: 2023-07-11 15:55:27 * @LastEditTime: 2023-07-12 15:29:07
* @FilePath: \dj-data-view\src\components\ViewTitle.vue * @FilePath: \dj-data-view\src\components\ViewTitle.vue
* @Description: 页面头部组件 * @Description: 页面头部组件
--> -->
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
<div class="tab-item first-tab" :class="currentMenu == 'home' ? 'active' : ''" @click.prevent="jump('home')"> <div class="tab-item first-tab" :class="currentMenu == 'home' ? 'active' : ''" @click.prevent="jump('home')">
<div class="tab-title">{{ firstTab.label }}</div> <div class="tab-title">{{ firstTab.label }}</div>
<img class="arrow-down" @click.stop="openSelect" src="@/assets/images/title-arrow.png" alt=""> <img class="arrow-down" @click.stop="openSelect" src="@/assets/images/title-arrow.png" alt="">
<SelectBox :isShow="isShow" :dataList="dataList" :boxWidth="'17.1vw'" isObject @selectVal="selectVal"></SelectBox> <SelectBox :isShow="isShow" @clickOutside="clickOutside" :dataList="dataList" :boxWidth="'17.1vw'" isObject @selectVal="selectVal"></SelectBox>
</div> </div>
<div class="tab-item other-tab" :class="currentMenu == 'operational' ? 'active' : ''" @click="jump('operational')">运营数据</div> <div class="tab-item other-tab" :class="currentMenu == 'operational' ? 'active' : ''" @click="jump('operational')">运营数据</div>
<div class="tab-item other-tab" :class="currentMenu == 'interior' ? 'active' : ''" @click="jump('interior')">内部数据</div> <div class="tab-item other-tab" :class="currentMenu == 'interior' ? 'active' : ''" @click="jump('interior')">内部数据</div>
...@@ -68,6 +68,9 @@ export default { ...@@ -68,6 +68,9 @@ export default {
if (val === 'home') _val = this.firstTab.value if (val === 'home') _val = this.firstTab.value
this.$bus.$emit('selectVal', _val) this.$bus.$emit('selectVal', _val)
this.isShow = false this.isShow = false
},
clickOutside() {
this.isShow = false
} }
} }
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!