JS生成登錄驗(yàn)證碼的實(shí)現(xiàn)示例
采用js生成登錄的驗(yàn)證碼
采用的技術(shù)點(diǎn)有html,css,JS,jQuery

HTML:
<div class="box_b"> <img src="./img/0775639c-c82c-4a29-937f-d2a3bae5151a.png" alt=""> <div class="register"> <h1>登錄</h1> <div class="register_r"> <span>賬號(hào):</span> <input type="text" placeholder="請(qǐng)輸入您的賬號(hào)"> </div> <div class="register_r"> <span>密碼:</span> <input type="password" placeholder="請(qǐng)輸入您的密碼" > </div> <div class="register_e"> <span>驗(yàn)證碼:</span> <input type="text" placeholder="請(qǐng)輸入驗(yàn)證碼驗(yàn)證"> <canvas id="c1" width="100" height="35" style="border:1px solid black"></canvas> </div> <div class="register_g"> <input type="checkbox"> <span>記住賬號(hào)密碼</span> </div> <button class="register_i">登錄</button> </div> </div>
css:
.box_b {
width: 100%;
height: 450px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: flex;
justify-content: space-around;
}
.box_b img {
width: 500px;
height: 550px;
}
.register {
width: 480px;
height: 400px;
background-color: #E0E0EF;
margin-top: 50px;
}
.register h1 {
text-align: center;
line-height: 80px;
}
.register span {
font-size: 20px;
}
.register_r {
width: 100%;
display: flex;
justify-content: space-evenly;
line-height: 60px;
text-align: center;
align-items: center;
}
.register_r input {
width: 300px;
height: 35px;
outline: none;
padding-left: 10px;
border: none;
}
.register_e {
width: 100%;
display: flex;
justify-content: space-evenly;
line-height: 60px;
text-align: center;
align-items: center;
}
.register_e input {
width: 140px;
height: 35px;
outline: none;
padding-left: 10px;
border: none;
margin-right: 30px;
}
.register_g {
display: flex;
align-items: center;
margin-left: 40px;
}
.register_g input {
width: 20px;
height: 20px;
margin-right: 7px;
}
.register_i {
background-color: #298DFB;
width: 84%;
line-height: 50px;
color: #fff;
margin-top: 5%;
border-radius: 5px;
text-align: center;
margin-left: 8%;
border: 1px #E4E7ED ridge;
font-size: 20px;
cursor: pointer;
}
#c1 {
vertical-align: middle;
box-sizing: border-box;
cursor: pointer;
margin-right: 10px;
}JS:
$(function() {
// 存放隨機(jī)的驗(yàn)證碼
var showNum = []
draw(showNum)
$("#c1").click(function() {
draw(showNum)
})
$(".register_i").click(function() {
var s = $("#inputValue").val().toLowerCase()
var s1 = showNum.join("")
if (s == s1) {
alert("提交成功")
} else {
alert("驗(yàn)證碼錯(cuò)誤")
}
draw(showNum)
})
// 封裝一個(gè)把隨機(jī)驗(yàn)證碼放在畫布上
function draw(showNum) {
// 獲取canvas
var canvas = $("#c1")
var ctx = canvas[0].getContext("2d")
// 獲取畫布的寬高
var canvas_width = canvas.width()
var canvas_height = canvas.height()
// 清空之前繪制的內(nèi)容
// 0,0清空的起始坐標(biāo)
// 矩形的寬高
ctx.clearRect(0, 0, canvas_width, canvas_height)
// 開始繪制
var scode =
"a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,1,2,3,4,5,6,7,8,9,"
var arrCode = scode.split(",")
var arrLength = arrCode.length
for (var i = 0; i < 4; i++) {
var index = Math.floor(Math.random() * arrCode.length)
var txt = arrCode[index] //隨機(jī)一個(gè)字符
showNum[i] = txt.toLowerCase() //轉(zhuǎn)化為小寫存入驗(yàn)證碼數(shù)組
// 開始控制字符的繪制位置
var x = 10 + 20 * i //每一個(gè)驗(yàn)證碼繪制的起始點(diǎn)x坐標(biāo)
var y = 20 + Math.random() * 8 // 起始點(diǎn)y坐標(biāo)
ctx.font = "bold 20px 微軟雅黑"
// 開始旋轉(zhuǎn)字符
var deg = Math.random * -0.5
// canvas 要實(shí)現(xiàn)繪制內(nèi)容具有傾斜的效果,必須先平移,目的是把旋轉(zhuǎn)點(diǎn)移動(dòng)到繪制內(nèi)容的地方
ctx.translate(x, y)
ctx.rotate(deg)
// 設(shè)置繪制的隨機(jī)顏色
ctx.fillStyle = randomColor()
ctx.fillText(txt, 0, 0)
// 把canvas復(fù)原
ctx.rotate(-deg)
ctx.translate(-x, -y)
}
for (var i = 0; i < 30; i++) {
if (i < 5) {
// 繪制線
ctx.strokeStyle = randomColor()
ctx.beginPath()
ctx.moveTo(Math.random() * canvas_width, Math.random() * canvas_height)
ctx.lineTo(Math.random() * canvas_width, Math.random() * canvas_height)
ctx.stroke()
}
// 繪制點(diǎn)
ctx.strokeStyle = randomColor()
ctx.beginPath()
var x = Math.random() * canvas_width
var y = Math.random() * canvas_height
ctx.moveTo(x, y)
ctx.lineTo(x + 1, y + 1)
ctx.stroke()
}
}
// 隨機(jī)顏色
function randomColor() {
var r = Math.floor(Math.random() * 256)
var g = Math.floor(Math.random() * 256)
var b = Math.floor(Math.random() * 256)
return `rgb(${r},${g},$)`
}
})到此這篇關(guān)于JS生成登錄驗(yàn)證碼的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)JS生成登錄驗(yàn)證碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js實(shí)現(xiàn)網(wǎng)頁檢測(cè)是否安裝了 Flash Player 插件
js實(shí)現(xiàn)網(wǎng)頁檢測(cè)是否安裝了 Flash Player 插件...2007-08-08
js輸出數(shù)據(jù)精確到小數(shù)點(diǎn)后n位代碼
要保留小數(shù)點(diǎn)后N位的問題,經(jīng)過一番思索,終于解決了,這篇文章主要介紹了js輸出數(shù)據(jù)精確到小數(shù)點(diǎn)后n位代碼,感興趣的朋友可以參考一下2016-07-07
JavaScript實(shí)現(xiàn)給按鈕加上雙重動(dòng)作的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)給按鈕加上雙重動(dòng)作的方法,可實(shí)現(xiàn)文本框元素值添加及頁面跳轉(zhuǎn)的功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
ztree獲取當(dāng)前選中節(jié)點(diǎn)子節(jié)點(diǎn)id集合的方法
這篇文章主要介紹了ztree獲取當(dāng)前選中節(jié)點(diǎn)子節(jié)點(diǎn)id集合的方法,實(shí)例分析了ztree的方法transformToArray使用技巧,需要的朋友可以參考下2015-02-02
微信開發(fā)之企業(yè)付款到銀行卡接口開發(fā)的示例代碼
這篇文章主要介紹了微信開發(fā)之企業(yè)付款到銀行卡接口開發(fā)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09
webpack4手動(dòng)搭建Vue開發(fā)環(huán)境實(shí)現(xiàn)todoList項(xiàng)目的方法
這篇文章主要介紹了webpack4手動(dòng)搭建Vue開發(fā)環(huán)境實(shí)現(xiàn)todoList項(xiàng)目的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05

