基于vue的驗證碼組件的示例代碼
最近在自己寫頁面,模仿思否論壇,然后寫登錄注冊UI的時候需要一個驗證碼組件. 去搜一下沒找到什么合適的,而且大多都是基于后端的,于是自己手寫一個。
演示

分析驗證碼組件
分析驗證碼功能
- 隨機出現(xiàn)的數(shù)字大小寫字母 (基礎(chǔ)功能)
- 不同的數(shù)字或者字母有不同的顏色 (功能優(yōu)化)
- 不同的數(shù)字或者字母有不同的字體大寫 (功能優(yōu)化)
- 不同的數(shù)字或者字母有不同的邊距 (功能優(yōu)化)
- 不同的數(shù)字或者字母有不同的傾斜角度 (功能優(yōu)化)
- 更多功能優(yōu)化...
分析組件功能
- 可以設(shè)置生成驗證碼的長度 (基本功能)
- 可以設(shè)置驗證碼組件的寬高 (基本功能)
編寫驗證碼組件
template
最外層div綁定點擊事件,點擊后刷新驗證碼。
span是單個驗證碼的載體,樣式動態(tài)綁定
<template>
<div class="ValidCode disabled-select" :style="`width:${width}; height:${height}`" @click="refreshCode">
<span v-for="(item, index) in codeList" :key="index" :style="getStyle(item)">{{item.code}}</span>
</div>
</template>
methods
refreshCode 刷新驗證碼的方法
createdCode 生成驗證碼的方法
getStyle 為每個元素生成動態(tài)的樣式
methods: {
refreshCode () {
this.createdCode()
},
createdCode () {
let len = this.length,
codeList = [],
chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz0123456789',
charsLen = chars.length
// 生成
for (let i = 0; i < len; i++) {
let rgb = [Math.round(Math.random() * 220), Math.round(Math.random() * 240), Math.round(Math.random() * 200)]
codeList.push({
code: chars.charAt(Math.floor(Math.random() * charsLen)), // 隨機碼
color: `rgb(${rgb})`, // 隨機顏色
fontSize: `1${[Math.floor(Math.random() * 10)]}px`, // 隨機字號
padding: `${[Math.floor(Math.random() * 10)]}px`, // 隨機內(nèi)邊距
transform: `rotate(${Math.floor(Math.random() * 90) - Math.floor(Math.random() * 90)}deg)` // 隨機旋轉(zhuǎn)角度
})
}
// 指向
this.codeList = codeList
// 將當(dāng)前數(shù)據(jù)派發(fā)出去
this.$emit('update:value', codeList.map(item => item.code).join(''))
},
// 動態(tài)綁定樣式
getStyle (data) {
return `color: ${data.color}; font-size: ${data.fontSize}; padding: ${data.padding}; transform: ${data.transform}`
}
}
完整代碼
使用
<valid-code :value.sync="validCode"></valid-code>
組件
<template>
<div class="ValidCode disabled-select" :style="`width:${width}; height:${height}`" @click="refreshCode">
<span v-for="(item, index) in codeList" :key="index" :style="getStyle(item)">{{item.code}}</span>
</div>
</template>
<script>
export default {
name: 'validCode',
props: {
width: {
type: String,
default: '100px'
},
height: {
type: String,
default: '40px'
},
length: {
type: Number,
default: 4
}
},
data () {
return {
codeList: []
}
},
mounted () {
this.createdCode()
},
methods: {
refreshCode () {
this.createdCode()
},
createdCode () {
let len = this.length,
codeList = [],
chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz0123456789',
charsLen = chars.length
// 生成
for (let i = 0; i < len; i++) {
let rgb = [Math.round(Math.random() * 220), Math.round(Math.random() * 240), Math.round(Math.random() * 200)]
codeList.push({
code: chars.charAt(Math.floor(Math.random() * charsLen)),
color: `rgb(${rgb})`,
fontSize: `1${[Math.floor(Math.random() * 10)]}px`,
padding: `${[Math.floor(Math.random() * 10)]}px`,
transform: `rotate(${Math.floor(Math.random() * 90) - Math.floor(Math.random() * 90)}deg)`
})
}
// 指向
this.codeList = codeList
// 將當(dāng)前數(shù)據(jù)派發(fā)出去
this.$emit('update:value', codeList.map(item => item.code).join(''))
},
getStyle (data) {
return `color: ${data.color}; font-size: ${data.fontSize}; padding: ${data.padding}; transform: ${data.transform}`
}
}
}
</script>
<style scoped lang="scss">
.ValidCode{
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
span{
display: inline-block;
}
}
</style>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue 實現(xiàn)LED數(shù)字時鐘效果(開箱即用)
這篇文章主要介紹了vue 實現(xiàn)LED數(shù)字時鐘效果(開箱即用),每一個數(shù)字由七個元素構(gòu)成,即每一個segment元素,本文給大家分享實現(xiàn)實例,感興趣的朋友一起看看吧2019-12-12
Vue實現(xiàn)導(dǎo)出Excel表格文件提示“文件已損壞無法打開”的解決方法
xlsx用于讀取解析和寫入Excel文件的JavaScript庫,它提供了一系列的API處理Excel文件,使用該庫,可以將數(shù)據(jù)轉(zhuǎn)換Excel文件并下載到本地,適用于在前端直接生成Excel文件,這篇文章主要介紹了Vue實現(xiàn)導(dǎo)出Excel表格,提示文件已損壞,無法打開的解決方法,需要的朋友可以參考下2024-01-01
antd form表單使用setFildesValue 賦值失效的解決
這篇文章主要介紹了antd form表單使用setFildesValue 賦值失效的解決方案,具有很好的參考價值,希望對大家有所幫助。2023-04-04
vue3中require報錯require is not defined問題及解決
這篇文章主要介紹了vue3中require報錯require is not defined問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06

