用vue写了些项目,但一直没有提取常用的祖件,现在尝试抽象提取出一些

记录

npm run dev 执行后自动打开浏览器

Q: npm run dev 执行后自动打开浏览器
A: 因为默认设置为false,需要自己设置 文档
下列两种方案 :

  1. package.json
    scripts中,在webpack-dev-server后加上 - -open
  2. config\index.js
    找到 autoOpenBrowser ,将false修改为true

picker

仿制weui的picker

通过外层盒子的touch事件改变内容的translate3d
遇到的问题:

  • 移动端上下切换卡顿

效果图

picker

源码

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
<template>
<div class="ix">
{{pageData}}
<div class="picker-bg" ref="bg" @touchstart="pStart" @touchmove="pMove" @touchend="pEnd">
<div class="select-ac"></div>
<div class="picker" ref="test">
<div class="box" ref="box">
<div v-for="(item,index) in picData.data" :id="'a' + index" :ref="'a' + index" :key="index"
@click="cIndex(index)">{{item}}
</div>
</div>
</div>
</div>
</div>

</template>
<script>
export default {
name: 'picker',
data: function () {
return {
picData: {
default: '1',
data: ['1', '2', '3', '4', '5', '6', '7', '1', '2', '3', '4', '5', '6', '7']
},
pageData: {
start: '',
end: '',
move: ''
}
}
},
created: function () {
}, // 创建时
beforeMount: function () {}, // 挂载之前
mounted: function () {}, // 挂载之后
beforeUpdate: function () {}, // 数据更新时调用,在渲染之前
updated: function () {}, // 数据更新后,渲染后调用(禁止)
beforeDestroy: function () {}, // 实例销毁前调用,解绑中间层的数据传输
destroyed: function () {}, // 实例销毁后调用
methods: {
cIndex (i) {
console.log('--i--', i)
},
pMove (e) {
// 获取当前位置
let newHeight = this.$refs.box.style.transform.split('px,')[1] || 0
// 获取上一次的位置
let oldHeight = this.pageData.start
// 计算活动距离
this.pageData.move = e.targetTouches[0].pageY
let tempHeight = e.targetTouches[0].pageY - this.pageData.start
let endHeight = Number(newHeight) + Number(tempHeight * 1.5)
console.log('--e--', newHeight, oldHeight, tempHeight, endHeight)
this.pageData.start = e.targetTouches[0].pageY
this.$refs.box.style.transform = `translate3d(0px, ${endHeight}px, 0px)`
e.preventDefault()
},
pEnd () {
// 当前transform
let oldHeight = this.$refs.box.style.transform.split('px,')[1] || 0
this.pageData.end = oldHeight
// options 高度,数量
let tempHeight = document.querySelectorAll('.picker .box div')[0].offsetHeight
let tempNumber = document.querySelectorAll('.picker .box div').length - 1
let index = Math.round(oldHeight / tempHeight)
console.log('--end--', index, tempNumber, tempHeight, this.picData.data.length)
if (index < -tempNumber) {
index = -tempNumber
}
if (index > 0) {
index = 0
}
this.$refs.box.style.transform = `translate3d(0px, ${index * 60}px, 0px)`
},
pStart (e) {
this.pageData.start = e.targetTouches[0].pageY
console.log('--start--', this.pageData.start)
}
} // 函数
}
</script>
<style lang='scss'>

.ix {
position: fixed;
width: 100vw;
bottom: 0;
z-index: 5;
}

$height: 60px;
.picker-bg {
display: block;
position: relative;
height: 5 * $height;
overflow: hidden;
width: 100%;
}

$acBg: rgba(86, 86, 86, 0.2);
$conBg: rgba(252, 252, 252, 0.70);
.select-ac {
width: 100%;
height: $height;
user-select: none;
background-color: $acBg;
position: absolute;
left: 0;
z-index: 3;
top: 2 * $height;
pointer-events: none;
&::after, &::before {
content: "";
display: block;
position: absolute;
background-color: $conBg;
width: 100%;
height: 500%;
pointer-events: auto;
}
&::before {
bottom: $height;
}
&::after {
top: $height;
}
}

.picker {
box-sizing: border-box;
position: relative;
/*overflow-y: auto;*/
&::-webkit-scrollbar {
width: 0;
}
.box {
user-select: none;
transition: all 0.4s;
display: flex;
flex-direction: column;
justify-content: center;
padding-top: 2 * $height;
padding-bottom: 2 * $height;
div {
user-select: none;
height: $height;
line-height: $height;
text-align: center;
font-size: 18px;
}
}

}
</style>

滚动条版picker

通过外层盒子设定高度,通过滚动可以做到类似效果

思路

整体View分为数据层,高亮选中层,整体背景层
高亮选中层使用before&after实现
通过css属性阻止非高亮区的点击事件冒泡
通过click获取当前高亮数据
监听滚动事件判断当前高亮的数据
缺点:
可能滚动到两个数据之间
(可以通过在滚动事件加入判断,让其滚动)

代码

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
<style lang='scss'>
$height: 60px;
.picker-bg {
display: block;
position: relative;
height: 5 * $height;
overflow: hidden;
width: 100%;
}
$acBg: rgba(86, 86, 86, 0.2);
$conBg: rgba(252, 252, 252, 0.70);
.select-ac {
width: 100%;
height: $height;
background-color: $acBg;
position: absolute;
left: 0;
top: 2 * $height;
pointer-events: none;
&::after, &::before {
content: "";
display: block;
position: absolute;
background-color: $conBg;
width: 100%;
height: 500%;
pointer-events: auto;
}
&::before {
bottom: $height;
}
&::after {
top: $height;
}
}

.picker {
box-sizing: border-box;
height: 100%;
overflow-y: auto;
&::-webkit-scrollbar{
width: 0;
}
.box {
display: flex;
flex-direction: column;
justify-content: center;
padding-top: 2 * $height;
padding-bottom: 2 * $height;
div {
height: $height;
line-height: $height;
text-align: center;
font-size: 18px;
}
}

}
</style>
<div class="picker-bg" >
<div class="select-ac"></div>
<div class="picker" ref="test" @scroll="sIndex">
<div class="box">
<div v-for="(item,index) in picData.data" :key="index" @click="cIndex(index)">{{item}}</div>
</div>
</div>
</div>
1
2
3
4
5
6
7
8
cIndex (i) {
console.log('--点击选中的:--', i)
}
sIndex () {
let tempHeight = document.querySelectorAll('.picker .box div')[0].offsetHeight
let index = Math.round(this.$refs.test.scrollTop / tempHeight)
console.log('--当前滚动位置选中的:--', index)
}

验证码

图中点字验证码

通过canvas绘制文字及背景,最终获取用户点击位置与文字位置匹配
遇到的问题:

  • 高分屏Canvas绘制文字及图片模糊
  • 坐标对比匹配(文字存在重叠)

效果图

canvas-yzm

源码

code.vue

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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<template>
<div class="checkCode" :class="{success:checkData.cData.flag}">
<span class="title">{{checkData.title}}</span>
<div class="content" @click="showCode">
<div class="anime">
<div class="one"></div>
</div>
<div v-show="!checkData.popShow && !checkData.cData.flag">{{checkData.text[0]}}</div>
<div v-show="checkData.popShow && !checkData.cData.flag">{{checkData.text[1]}}</div>
<div v-show="!checkData.popShow && checkData.cData.flag">{{checkData.text[2]}}</div>
</div>
<div class="dialog-bg" @click.self.stop="closeCode" v-show="checkData.popShow"></div>
<div class="dialog l-box" v-show="checkData.popShow && !checkData.cData.flag">
<div class="title">
<span v-if="checkData.cData.tipStatus">{{checkData.cData.tip[0]}}</span>
<span v-else class="warn">{{checkData.cData.tip[1]}}</span>
<span class="card-bg">{{checkData.cData.text}}</span>
</div>
<div class="content">
<canvas id="yzm-canvas" @click="addNumber" :width="parseInt(checkData.cData.style.width) * 2 + 'px' "
:height="parseInt(checkData.cData.style.height) * 2 + 'px' " :style="checkData.cData.style"></canvas>
<div v-for="(item,index) in checkData.cData.clickData" class="number" @click="removeNumber(index)"
:data-index="index + 1"
:key="index" :style="{left: item.x + 'px', top: item.y + 35 + 'px'}"></div>
</div>
<div class="footer">
<i class="iconfont icon-refresh tip" data-tip="刷新验证码" @click="reFont"></i>
<button @click="checkCfData">验证</button>
</div>
</div>
</div>
</template>
<script>
import DrawCode from './drawCode'

export default {
name: 'checkCode',
data: function () {
return {
checkData: {
title: '验证码',
text: ['点击进行验证', '正在验证', '验证成功'],
popShow: false,
cData: {
flag: false,
text: '默认成语',
tipStatus: true,
tip: ['请按顺序点击图中文字', '验证错误,请重新验证'],
style: {
width: '280px',
height: '200px'
},
clickData: [],
fontData: []
}
}
}
},
created: function () {}, // 创建时
beforeMount: function () {}, // 挂载之前
mounted: function () {}, // 挂载之后
beforeUpdate: function () {}, // 数据更新时调用,在渲染之前
updated: function () {}, // 数据更新后,渲染后调用(禁止)
beforeDestroy: function () {}, // 实例销毁前调用,解绑中间层的数据传输
destroyed: function () {}, // 实例销毁后调用
methods: {
showCode () {
if (this.checkData.cData.flag) { return false }
this.checkData.popShow = true
return this.reFont()
},
closeCode () {
this.checkData.popShow = false
},
addNumber (e) {
this.checkData.cData.tipStatus = true
if (this.checkData.cData.clickData.length >= 4) {
return false
}
let x = e.offsetX
let y = e.offsetY
this.checkData.cData.clickData.push({x, y})
if (this.checkData.cData.clickData.length === 4) {
return this.checkCfData()
}
},
removeNumber (i) {
this.checkData.cData.clickData.splice(i)
},
drawFont (data) {
let temp = new DrawCode(data)
temp.start()
this.checkData.cData.fontData = temp.fontData
this.checkData.cData.scope = temp.scope
this.checkData.cData.text = temp.text.join('')
},
reFont (flag) {
let tempStr = [['狡兔三窟', '免库'], ['长袖善舞', '柚'], ['匪夷所思', '篚'], ['飞扬跋扈', '杨拔'], ['多事之秋', '是又']]
this.checkData.cData.clickData = []
this.checkData.cData.flag = false
if (flag !== true) {
this.checkData.cData.tipStatus = true
} else {
this.checkData.cData.tipStatus = false
}
this.drawFont({
text: tempStr[parseInt(Math.random() * tempStr.length)]
})
},
checkCfData () {
if (this.checkData.cData.clickData.length !== 4) {
this.checkData.cData.flag = false
return false
}
let flag = this.checkData.cData.fontData.map((v) => { return {x: v.x / 2, y: v.y / 2} }).every((value, index) => {
let booleanX = value.x <= this.checkData.cData.clickData[index].x && this.checkData.cData.clickData[index].x <= (parseInt(value.x) + this.checkData.cData.scope / 2)
let booleanY = value.y <= this.checkData.cData.clickData[index].y && this.checkData.cData.clickData[index].y <= (parseInt(value.y) + this.checkData.cData.scope / 2)
return booleanX || booleanY
})
this.checkData.cData.flag = flag
if (flag) {
this.checkData.popShow = false
} else {
this.checkData.cData.tipStatus = false
return this.reFont(true)
}
}
} // 函数
}
</script>
<style lang='scss' scoped>
/* click-box */
.yzm-img{
.bg{}
.card{
width: 50px;
height: 50px;
background-color: gold;
position: relative;
&::before{
content: "";
display: block;
left: 50%;
width: 20px;
height: 20px;
border-radius: 50% 50% 0 0;
background-color: black;
top: -10px;
margin-left: -10px;
position: absolute;
}
&::after{
content: "";
display: block;
left: 50%;
width: 20px;
height: 20px;
border-radius: 50%;
position: absolute;
background-color: red;
bottom: -10px;
margin-left: -10px;
}
}
}
.checkCode {
/* 基本参数设置 */
$success-color: #42DD18; // 成功色调
$height: 50px; // click-box 最小高度
$gap: 15px; // click-box 间距
/* 验证成功 */
&.success {
> .content {
border: 1px solid $success-color;
color: $success-color;
pointer-events: none;
.anime .one::before {
background-color: $success-color;
}
}
}
/* 禁止复制 */
* {
user-select: none;
}
width: 100%;
box-sizing: border-box;
display: flex;
flex-direction: row;
align-items: center;
min-height: $height;
margin: #{$gap * 0.5} 0;
position: relative;
> .title {
padding: 0 $gap;
}
> .content {
max-width: 200px;
border: 1px solid #e7e7e7;
flex: 1;
height: $height;
margin-right: $gap;
display: flex;
flex-direction: row;
align-items: center;
.anime {
min-width: $height;
margin-left: $gap;
$w: 12px;
.one {
display: block;
width: $w * 3;
height: $w * 3;
border-radius: 50%;
background-color: #DEE9FA;
position: relative;
animation: zoomIn 1.2s infinite alternate ease-in;
transition: all 1.2s;
@keyframes zoomIn {
to {
transform: scale(0.6);
}
}
@keyframes zoomOut {
to {
transform: scale(1.5);
}
}
&::before {
content: "";
display: block;
position: absolute;
width: $w;
height: $w;
border-radius: 50%;
background-color: #5D91F4;
left: $w;
top: $w;
animation: zoomOut 2.4s infinite alternate 1s ease-in;
}
}
}
}
.dialog-bg {
position: fixed;
left: 0;
top: 0;
height: 100vh;
width: 100vw;
z-index: 4;
}
.dialog {
$dW: 320px;
$dH: 200px;
min-width: $dW;
min-height: $dH;
position: absolute;
border: 1px solid #e6e6e6;
background-color: white;
z-index: 5;
top: $height;
margin-left: $height * 2.9;
margin-top: -#{$height * 0.7};
$width: 8px;
&::before {
content: "";
height: 0px;
width: 0px;
position: absolute;
left: -17px;
top: 5px;
z-index: 0;
border-left: $width solid transparent;
border-top: $width solid transparent;
border-bottom: $width solid transparent;
border-right: $width solid rgba(63, 63, 63, 0.1);
}
&::after {
content: "";
height: 0px;
width: 0px;
position: absolute;
left: -14px;
top: 5px;
z-index: 1;
border-left: $width solid transparent;
border-top: $width solid transparent;
border-bottom: $width solid transparent;
border-right: $width solid white;
}
@media screen and (max-width: 480px) {
& {
left: calc(50% - 160px);
margin: 0;
top: 70px;
&::before {
left: auto;
top: -17px;
transform: rotateZ(90deg);
}
&::after {
left: auto;
top: -15px;
transform: rotateZ(90deg);
}
}
}
}
.l-box {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
canvas {
border: 1px solid #e6e6e6;
}
.warn {
color: #ff8a00;
}
.title {
height: 50px;
line-height: 50px;
display: flex;
flex-direction: row;
align-items: center;
.card-bg {
border: 1px solid #e6e6e6;
box-sizing: border-box;
height: 30px;
line-height: 30px;
background-color: deepskyblue;
color: white;
font-weight: bold;
margin: 0 10px;
padding: 0 10px;
letter-spacing: 4px;
}
}
.content {

}
.footer {
margin: 5px 0;
width: 100%;
height: 40px;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-around;
i {
font-size: 30px;
color: #b3b3b3;
}
button {
height: 40px;
width: 100px;
color: white;
background-color: deepskyblue;
border: 0;
font-size: 18px;
}
}
}
.tip {
position: relative;
&:hover {
&::after {
content: attr(data-tip);
}
}
&::after {
$w: 50px;
display: block;
position: absolute;
font-size: 16px;
min-width: $w;
background: white;
border: 1px solid #e6e6e6;
margin-top: - #{$w + 10px};
margin-left: - #{$w / 2};
white-space: nowrap;
padding: 3px;
}
}
.number {
width: 30px;
height: 30px;
border-radius: 50%;
color: white;
text-align: center;
line-height: 30px;
position: absolute;
background: #007cff;
box-shadow: 0 0 2px 1px white;
&::before {
content: attr(data-index);
font-size: 20px;
}
}
}
</style>

DrawCode.js

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
/**
* 1. 准备画布背景
* 0. 清空画布
* 1. 获取画布大小
* 2. 获取背景
* 2. 绘制文字(坐标,css,文字)
* 0. 获取文字
* 1. 获取css
* 2. 获取坐标
* 3. 绘制
* 4. 绘制干扰字符
* -------
* 3. 判断用户操作
* 0. 判断点击数量
* 1. 判断点击位置
* 4. 用户操作
* 0. 添加标识
* 1. 刷新验证码
*
* 防止坐标重复的方法
* 0. 先提取一个有所有坐标的数组 (X)
* 1. 随机,迭代与抽取出的坐标比较 (*)
**/
class DrawCode {
constructor ({
selector = 'yzm-canvas',
imgURL = require('@/assets/yzm/17.jpg'),
text = ['负荆请罪', '清醉'],
prob = '0.85',
scope = '72',
style = {
width: '280px',
height: '200px',
font: 'oblique small-caps bold 72px Arial',
color: 'white'
}
} = {}) {
this.ctx = document.getElementById(selector).getContext('2d')
this.imgURL = imgURL
this.text = [...text[0]]
this.mix = [...text[1]]
this.prob = prob
this.width = parseInt(style.width) * 2
this.height = parseInt(style.height) * 2
this.font = style.font
this.color = style.color
this.fontData = []
this.scope = parseInt(scope)
}

getRanXY () {
let x = parseInt(Math.random() * (this.width - this.scope))
let y = parseInt(Math.random() * (this.height - this.scope))
let flag = this.fontData.some((value) => {
let booleanX = value.x <= x && x <= (parseInt(value.x) + this.scope)
let booleanY = value.y <= y && y <= (parseInt(value.y) + this.scope)
return booleanX || booleanY
})
if (flag) {
return this.getRanXY()
} else {
return {x, y}
}
}

start () {
let img = new Image()
img.onload = () => {
this.ctx.drawImage(img, 0, 0, this.width, this.height)
this.ctx.font = this.font
this.ctx.fillStyle = this.color
this.ctx.textAlign = 'start'
this.ctx.textBaseline = 'top'
for (let i in this.text) {
let tempXY = this.getRanXY()
this.ctx.fillText(this.text[i], tempXY.x, tempXY.y)
this.fontData.push(tempXY)
}
if (Math.random() > this.prob) {
let tempXY = this.getRanXY()
this.ctx.fillText(this.mix[parseInt(Math.random() * this.mix.length)], tempXY.x, tempXY.y)
}
}
img.src = this.imgURL
}
}

export default DrawCode