index.vue
4.13 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<template>
<div class="slider">
<div ref="slider" class="slider" :style="'width:'+width+'px;background:'+bgcolor">
<div
v-for="(item,index) in data"
:key="index"
class="sbox"
:class="'s'+index"
:index="index"
:style="'width:'+((1*width-30)*parseInt(item.bl.replace('%',''))/100+30)+'px;background:'+item.color+';z-index:'+(8-index)"
>
<div class="round-bg" @mousedown="down($event)">
<span class="bl">{{ item.bl }}</span>
<div class="round" :style="'background:'+item.color" />
<span class="jb">{{ item.name }}</span>
</div>
</div>
<div class="perfect">
<span class="bl">100%</span>
<div class="icon_img"><img :src="require('@/assets/images/IMG_1789.png')" alt=""></div>
<span class="jb">优秀</span>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'Slider',
props: {
data: {
type: Array,
default: function() {
return []
}
},
// eslint-disable-next-line vue/require-default-prop
width: [String, Number],
bgcolor: {
type: String,
default: ''
}
},
data() {
return {
startX: 0,
sliderFlag: false,
prevWidth: '',
nextWidth: '',
obj: {}
}
},
methods: {
down(event) {
event.preventDefault()
this.startX = event.clientX
this.sliderFlag = true
this.getEle(event.target) // 获取父级元素
console.log(this.obj.getAttribute('index'))
if (this.obj.getAttribute('index') === '0') {
this.prevWidth = '30px'
this.nextWidth = parseInt(this.obj.nextElementSibling.style.width.replace('px', '') - 30) + 'px'
} else if (this.obj.getAttribute('index') === (this.data.length - 1 + '')) {
this.prevWidth = parseInt(this.obj.previousElementSibling.style.width.replace('px', '')) + 30 + 'px'
this.nextWidth = this.$refs.slider.offsetWidth + 'px'
} else {
this.prevWidth = parseInt(this.obj.previousElementSibling.style.width.replace('px', '')) + 30 + 'px'
this.nextWidth = parseInt(this.obj.nextElementSibling.style.width.replace('px', '') - 30) + 'px'
}
window.addEventListener('mousemove', this.move)
window.addEventListener('mouseup', this.up)
},
move(event) {
let _width = parseInt(this.obj.style.width.replace('px', ''))
_width += event.clientX - this.startX
if (_width > parseInt(this.nextWidth.replace('px', '')) || _width < parseInt(this.prevWidth.replace('px', ''))) return
const bl = ((_width - 30) * 100 / (this.$refs.slider.offsetWidth - 30)).toFixed(0) + '%'
this.obj.childNodes[0].childNodes[0].innerText = bl
this.startX = event.clientX
this.obj.style.width = _width + 'px'
},
up(event) {
this.sliderFlag = false
window.removeEventListener('mousemove', this.move)
window.removeEventListener('mouseup', this.up)
const _index = parseInt(this.obj.getAttribute('index'))
const bl = this.obj.childNodes[0].childNodes[0].innerText
this.data[_index].bl = bl
this.$emit('change', this.data)
},
getEle(obj) {
const _this = this
if (obj.getAttribute('class').indexOf('sbox') > -1) {
_this.obj = obj
} else {
this.getEle(obj.parentNode)
}
}
}
}
</script>
<style scoped>
.slider{
width:400px;
height:20px;
border-radius: 10px 0 0 10px;
background: #888;
position: relative;
margin:20px 0;
}
.sbox{
position: absolute;
left: 0;
top:0;
height:20px;
width:100px;
background: #0ff;
border-radius: 10px;
}
.round-bg{
width:30px;
height:30px;
border-radius: 15px;
background: #fff;
border:1px solid #ccc;
position: absolute;
right:0;
top:-5px;
}
.round{
width:20px;
height:20px;
border-radius: 12px;
position: absolute;
top:4px;
left:4px;
background: #0ff;
}
.bl{
font-size:12px;
position: absolute;
top:-14px;
}
.jb{
font-size:12px;
position: absolute;
bottom:-20px;
white-space: nowrap;
}
.perfect{
width:30px;
position: absolute;
top:-7px;
right:-30px;
}
.perfect img{
width:100%;
}
</style>