SelectBox.vue
3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<!--
* @Description: 下拉框组件
* @Author: huyue
* @Date: 2023-07-05 15:11:53
* @LastEditors: huyue
* @LastEditTime: 2023-07-17 18:18:37
-->
<template>
<div v-click-outside="handleClickOutside" @mouseleave="handleMouseLeave" v-show="isShow" class="select-box" :style="{'width': boxWidth}">
<slot name="search"></slot>
<div v-if="dataList.length > 0" class="select-scroll">
<div class="select-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 v-else class="empty">
<slot name="empty"></slot>
</div>
</div>
</template>
<script type='text/ecmascript-6'>
export default {
name: 'SelectBox',
props: {
isShow: {
type: Boolean,
default: false
},
dataList: {
type: Array,
default: () => []
},
// dataList里面的值是否是对象
isObject: {
type: Boolean,
default: false
},
labelName: {
type: String,
default: 'label'
},
valueName: {
type: String,
default: 'value'
},
// 下拉框的宽度设置
boxWidth: {
type: String
},
defaultValue: {
type: String || Number
}
},
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() {
return {
value: '',
empty: '暂无数据'
}
},
mounted() {
const { valueName, isObject, dataList, defaultValue } = this
this.value = defaultValue || (isObject ? dataList[0][valueName] : dataList[0])
},
methods: {
select(item) {
const { valueName, isObject } = this
this.value = isObject ? item[valueName] : item
this.$emit('selectVal', item)
},
handleMouseLeave() {
// 在此处处理鼠标移出事件的逻辑
},
handleClickOutside() {
this.$emit('clickOutside')
// 在此处处理鼠标点击事件的逻辑
}
}
}
</script>
<style scoped lang='scss'>
.select-box {
position: absolute;
top: 4.8vh;
left: 0;
background-color: #051526;
padding: 16px 0;
box-sizing: border-box;
z-index: 1;
border: 2px solid;
border-image: linear-gradient(90deg, #074989, #30DCFF, #074989) 1;
.select-item {
margin-bottom: 8px;
color: #C9C9C9;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
padding: 0 1.39vw;
cursor: pointer;
&:hover {
color: #5894FF;
background-image: -webkit-linear-gradient(left, #24D8FF, #7787FF);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
&:last-child {
margin-bottom: 0;
}
&.active {
color: #5894FF;
background-image: -webkit-linear-gradient(left, #24D8FF, #7787FF);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
}
}
.select-scroll {
max-height: 23.33vh;
overflow-y: auto;
}
</style>
<style>
::-webkit-scrollbar {
width: 5px; /* 设置滚动条的宽度 */
}
::-webkit-scrollbar-track {
background-image: linear-gradient(90deg, #0A214A 0%, #061422 50%, #0A214A 100%);
/* 设置滚动条的轨道背景颜色 */
}
::-webkit-scrollbar-thumb {
border-radius: 5px;
background-color: rgba(244, 244, 244, 0.26); /* 设置滑块的颜色 */
}
</style>