JcRange.vue
3.86 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<template>
<div class="jc-component__range">
<div v-loading="loading" class="jc-range" :class="rangeStatus ? 'success' : ''">
<i @mousedown="rangeMove" :class="rangeStatus ? successIcon : startIcon"></i>
{{ rangeStatus ? successText : startText }}
</div>
</div>
</template>
<script>
import * as login from '@/api/login'
export default {
props: {
// 成功之后的函数
successFun: {
type: Function,
},
//成功图标
successIcon: {
type: String,
default: 'el-icon-check',
},
//成功文字
successText: {
type: String,
default: '验证成功',
},
//开始的图标
startIcon: {
type: String,
default: 'el-icon-right',
},
//开始的文字
startText: {
type: String,
default: '请按住滑块,拖动到最右边',
},
//失败之后的函数
errorFun: {
type: Function,
},
//或者用值来进行监听
status: {
type: String,
},
},
data () {
return {
loading: false,
disX: 0,
rangeStatus: false,
str: {},
}
},
methods: {
moveFn (e) {
let ele = e.target
ele.style.transition = '.5s all'
ele.style.transform = 'translateX(0)'
this.rangeStatus = false
// 执行成功的函数
this.errorFun()
},
//滑块移动
rangeMove (e) {
this.str = e
let ele = e.target
let startX = e.clientX
let eleWidth = ele.offsetWidth
let parentWidth = ele.parentElement.offsetWidth
let MaxX = parentWidth - eleWidth
if (this.rangeStatus) {
//不运行
return false
}
document.onmousemove = (e) => {
let endX = e.clientX
this.disX = endX - startX
if (this.disX <= 0) {
this.disX = 0
}
if (this.disX >= MaxX - eleWidth) {
//减去滑块的宽度,体验效果更好
this.disX = MaxX
}
ele.style.transition = '.1s all'
ele.style.background = 'red!important'
ele.style.transform = 'translateX(' + this.disX + 'px)'
e.preventDefault()
}
document.onmouseup = async () => {
if (this.disX !== MaxX) {
ele.style.transition = '.5s all'
ele.style.transform = 'translateX(0)'
// 执行成功的函数
this.errorFun && this.errorFun()
}
else {
this.successFun && this.successFun()
this.rangeStatus = true
this.loading = true
if (this.status) {
this.$parent[this.status] = true
}
}
document.onmousemove = null
document.onmouseup = null
}
},
},
}
</script>
<style lang="scss" scoped>
@mixin jc-flex {
display: flex;
justify-content: center;
align-items: center;
}
.jc-component__range {
.jc-range {
background-color: #F7FAFE;
border: 1px solid #D8E0E9;
position: relative;
transition: 1s all;
user-select: none;
color: #9B9B9B;
display: flex;
// justify-content: center;
padding-left: 50px;
align-items: center;
height: 40px;
/*no*/
&.success {
background-color: #D2F5EF;
color: #4caf50;
border: 1px solid #99D5D0;
font-size: 16px;
justify-content: center;
padding-left: 0px;
i {
color: #ffffff;
background-color: #51CCBF;
}
}
i {
position: absolute;
left: 0;
width: 38px;
/*no*/
height: 40px;
background-color: #fff;
border: 1px solid #d8d8d8;
cursor: pointer;
font-size: 14px;
color: #829ABA;
@include jc-flex;
}
}
}
</style>
<style lang="scss" scoped>
/deep/.el-loading-spinner {
top: 62%;
.circular {
height: 35px !important;
width: 30px !important;
}
}
/deep/.el-loading-mask {
background-color: rgba(255, 255, 255, 1);
}
</style>