基于vue的驗(yàn)證碼組件的示例代碼
最近在自己寫頁面,模仿思否論壇,然后寫登錄注冊(cè)UI的時(shí)候需要一個(gè)驗(yàn)證碼組件. 去搜一下沒找到什么合適的,而且大多都是基于后端的,于是自己手寫一個(gè)。
演示
分析驗(yàn)證碼組件
分析驗(yàn)證碼功能
- 隨機(jī)出現(xiàn)的數(shù)字大小寫字母 (基礎(chǔ)功能)
- 不同的數(shù)字或者字母有不同的顏色 (功能優(yōu)化)
- 不同的數(shù)字或者字母有不同的字體大寫 (功能優(yōu)化)
- 不同的數(shù)字或者字母有不同的邊距 (功能優(yōu)化)
- 不同的數(shù)字或者字母有不同的傾斜角度 (功能優(yōu)化)
- 更多功能優(yōu)化...
分析組件功能
- 可以設(shè)置生成驗(yàn)證碼的長度 (基本功能)
- 可以設(shè)置驗(yàn)證碼組件的寬高 (基本功能)
編寫驗(yàn)證碼組件
template
最外層div綁定點(diǎn)擊事件,點(diǎn)擊后刷新驗(yàn)證碼。
span是單個(gè)驗(yàn)證碼的載體,樣式動(dòng)態(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 刷新驗(yàn)證碼的方法
createdCode 生成驗(yàn)證碼的方法
getStyle 為每個(gè)元素生成動(dòng)態(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)), // 隨機(jī)碼 color: `rgb(${rgb})`, // 隨機(jī)顏色 fontSize: `1${[Math.floor(Math.random() * 10)]}px`, // 隨機(jī)字號(hào) padding: `${[Math.floor(Math.random() * 10)]}px`, // 隨機(jī)內(nèi)邊距 transform: `rotate(${Math.floor(Math.random() * 90) - Math.floor(Math.random() * 90)}deg)` // 隨機(jī)旋轉(zhuǎn)角度 }) } // 指向 this.codeList = codeList // 將當(dāng)前數(shù)據(jù)派發(fā)出去 this.$emit('update:value', codeList.map(item => item.code).join('')) }, // 動(dòng)態(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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue?使用?ElementUi?el-upload?手動(dòng)上傳文件限制上傳文件類型大小同名等進(jìn)行限制
個(gè)人在做文件上傳功能的時(shí)候,踩過不少的坑,特在此記錄下,本文介紹Vue使用?ElementUi?el-upload?手動(dòng)上傳文件限制上傳文件類型大小同名等進(jìn)行限制問題,感興趣的朋友一起看看吧2024-02-02解決vue前端rsa加密遇到的問題message too long for RS
這篇文章主要介紹了解決vue前端rsa加密遇到的問題message too long for RSA,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07