ImageVerification.vue
7.67 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<template>
<div
v-if="visible"
class="slidingPictures"
>
<div class="sliding-pictures">
<div class="vimg">
<canvas id="sliderBlock" />
<canvas id="codeImg" />
</div>
<div class="slider">
<div
id="tempBlock"
class="button el-icon-s-grid"
@mousedown.prevent="drag"
@touchstart="mobileDrag"
/>
<div
class="track"
:class="{ pintuTrue: puzzle }"
>
{{ tips }}
</div>
</div>
<div class="operation">
<span
title="关闭验证码"
class="el-icon-circle-close"
@click="$emit('close')"
/>
<span
title="刷新验证码"
class="el-icon-refresh-left"
@click="$emit('refresh')"
/>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'ImageVerification',
props: {
visible: {
type: Boolean,
required: true
},
bgImage: {
type: String,
required: true
},
sliderImage: {
type: String,
required: true
},
yHeight: {
type: Number,
required: true
}
},
data() {
return {
tips: '拖动左边滑块完成上方拼图',
puzzle: false
}
},
watch: {
visible(e) {
if (e === true) {
this.$nextTick(() => {
this.draw()
})
this.puzzle = false
}
},
bgImage: {
handler: function (val, oldval) {
if (this.visible && oldval !== '' && val !== oldval) {
this.$nextTick(() => {
this.draw()
})
}
}
}
},
methods: {
mobileDrag(e) {
const dom = e.target // dom元素
const slider = document.querySelector('#sliderBlock') // 滑块dom
const tempBlock = document.querySelector('#tempBlock')
const downCoordinate = { x: e.touches[0].clientX, y: e.touches[0].clientY }
let x = 0
tempBlock.startX = e.touches[0].clientX - tempBlock.offsetLeft
slider.startX = e.touches[0].clientX - slider.offsetLeft
const move = moveEV => {
// 设置滑动按钮不能超出父级div
tempBlock.style.left = moveEV.touches[0].clientX - tempBlock.startX + 'px'
if (moveEV.touches[0].clientX - tempBlock.startX <= 0) {
tempBlock.style.left = 0 + 'px'
}
if (moveEV.touches[0].clientX - tempBlock.startX >= 230) {
tempBlock.style.left = 230 + 'px'
}
// 设置滑块不能超出父级div
slider.style.left = moveEV.touches[0].clientX - slider.startX + 'px'
if (moveEV.touches[0].clientX - slider.startX <= 0) {
slider.style.left = 0 + 'px'
}
if (moveEV.touches[0].clientX - slider.startX >= 225) {
slider.style.left = 225 + 'px'
}
x = moveEV.touches[0].clientX - downCoordinate.x
if (x >= 281 || x <= 0) return false
}
const up = e => {
document.removeEventListener('touchmove', move)
document.removeEventListener('touchend', up)
// 滑块和按钮回到起始位置
slider.style.left = ''
dom.style.left = ''
// 采用点击距离和最终离开距离计算滑动距离
const movex = e.changedTouches[0].clientX - downCoordinate.x
this.$emit('getMoveDistance', movex)
}
document.addEventListener('touchmove', move)
document.addEventListener('touchend', up)
},
// 鼠标按下
drag(e) {
const dom = e.target // dom元素
const slider = document.querySelector('#sliderBlock') // 滑块dom
const tempBlock = document.querySelector('#tempBlock')
const downCoordinate = { x: e.x, y: e.y }
let x = 0
tempBlock.startX = e.clientX - tempBlock.offsetLeft
slider.startX = e.clientX - slider.offsetLeft
const move = moveEV => {
// 设置滑动按钮不能超出父级div
tempBlock.style.left = moveEV.clientX - tempBlock.startX + 'px'
if (moveEV.clientX - tempBlock.startX <= 0) {
tempBlock.style.left = 0 + 'px'
}
if (moveEV.clientX - tempBlock.startX >= 230) {
tempBlock.style.left = 230 + 'px'
}
// 设置滑块不能超出父级div
slider.style.left = moveEV.clientX - slider.startX + 'px'
if (moveEV.clientX - slider.startX <= 0) {
slider.style.left = 0 + 'px'
}
if (moveEV.clientX - slider.startX >= 225) {
slider.style.left = 225 + 'px'
}
x = moveEV.x - downCoordinate.x
if (x >= 281 || x <= 0) return false
}
const up = e => {
document.removeEventListener('mousemove', move)
document.removeEventListener('mouseup', up)
// 滑块和按钮回到起始位置
slider.style.left = ''
dom.style.left = ''
// 采用点击距离和最终离开距离计算滑动距离
const movex = e.x - downCoordinate.x
this.$emit('getMoveDistance', movex)
// this.handleSuccess(movex)
}
document.addEventListener('mousemove', move)
document.addEventListener('mouseup', up)
},
draw() {
const mainDom = document.querySelector('#codeImg')
const bg = mainDom.getContext('2d')
const height = mainDom.height
const blockDom = document.querySelector('#sliderBlock')
const block = blockDom.getContext('2d')
blockDom.height = height
mainDom.height = height
blockDom.width = 281
mainDom.width = 281
// 背景图片
const bgImage = document.createElement('img')
bgImage.style.objectFit = 'scale-down'
bgImage.src = this.bgImage
bgImage.onload = function () {
console.log('背景图片宽度', bgImage.width)
console.log('背景图片高度', bgImage.height)
bg.drawImage(bgImage, 0, 0, bgImage.width, bgImage.height)
}
// 滑块图片
const sliderImage = document.createElement('img')
sliderImage.src = this.sliderImage
sliderImage.onload = () => {
console.log('滑块高度', this.yHeight)
block.drawImage(sliderImage, 0, this.yHeight, sliderImage.width, sliderImage.height)
}
}
}
}
</script>
<style>
.slidingPictures {
padding: 0;
border-radius: 2px;
display: flex;
}
</style>
<style scoped lang="scss">
.sliding-pictures {
position: relative;
.vimg {
width: 280px;
height: 171px;
#codeImg,
#sliderBlock {
width: inherit;
height: inherit;
}
#sliderBlock {
position: absolute;
z-index: 4000;
}
}
.slider {
position: relative !important;
width: 100%;
height: 65px;
border-bottom: #c7c9d0 1px solid;
display: flex;
align-items: center;
justify-content: flex-start;
.track {
margin-left: 7px;
width: 270px;
height: 38px;
background: rgba(28, 136, 188, 0.5);
border-radius: 25px;
font-size: 14px;
line-height: 38px;
padding-right: 15px;
padding-left: 70px;
}
.pintuTrue {
background: #67c23a;
color: #ffffff;
}
.button {
position: absolute;
left: 0;
top: 7.5px;
width: 50px;
height: 50px;
line-height: 48px;
background: #ffffff;
box-shadow: #b9bdc8 0 0 3px;
border-radius: 50%;
text-align: center;
font-size: 28px;
color: #3e5d8b;
&:hover {
color: #2181bd;
}
}
}
.operation {
width: 100%;
height: 40px;
> span {
color: #9fa3ac;
display: inline-block;
width: 40px;
font-size: 25px;
line-height: 40px;
text-align: center;
&:hover {
background: #e2e8f5;
}
}
}
}
</style>