小編給大家分享一下js+canvas如何實(shí)現(xiàn)驗(yàn)證碼功能,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
公司主營(yíng)業(yè)務(wù):網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)公司是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)公司推出揭東免費(fèi)做網(wǎng)站回饋大家。
剛剛開始接觸canvas,寫個(gè)驗(yàn)證碼小功能練練手,實(shí)現(xiàn)效果圖如下:
主要代碼如下:
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <link rel="stylesheet" href="index.css" rel="external nofollow" > </head> <body> <div class="wrapper"> <div class="inputBox"> <input type="text" class = 'inputCaptcha' placeholder = "請(qǐng)輸入驗(yàn)證碼"> <span class="captchaIcon"></span> </div> <p class="errorText"></p> <div class="canvasBox"> <div class="imageBox"> <canvas width =300 height=80 id = 'canvasCaptcha'></canvas> <input type="button" class='refresh'> </div> </div> <button class="captchaSubmit">submit</button> </div> <script src='./jquery.js'></script> <script src = './index.js'></script> </body> </html>
css
* { margin: 0; padding: 0; } .wrapper { width: 345px; border: 1px solid #ccc; border-radius: 10px; padding: 20px 10px; margin: 30px 30px; } .inputBox { width: 345px; margin: 0 auto 10px; position: relative; } .inputBox .inputCaptcha { display: inline-block; height: 50px; width: 86%; text-indent: 1em; border: 1px solid #ddd; border-radius: 5px; } .inputBox .captchaIcon { display: none; position: absolute; top: 50%; right: 0px; margin-top: -16px; width: 32px; height: 32px; background-size: 100% 100%; } .errorText { width: 345px; margin: 0 auto; font-size: 12px; color: red; display: none; } .canvasBox { width: 345px; margin: 10px auto; position: relative; } #canvasCaptcha { border-radius: 10px; } .canvasBox .refresh { width: 34px; height: 34px; position: absolute; right: 0px; top: 50%; margin-top: -17px; border: 0; border-radius: 6px; background-image: url('./images/update.png'); background-size: cover; } .captchaSubmit { padding: 10px 20px; background-color: #62b900; border: 0; font-size: 20px; border-radius: 5px; color: #fff; }
js
var arr = [0,1,2,3,4,5,6,7,8,9]; for(var i = 65;i < 122;i++){ if(i>90&&i<97){ continue; } arr.push(String.fromCharCode(i)); } //每個(gè)驗(yàn)證碼可能出現(xiàn)的字母或數(shù)字(區(qū)分大小寫) var len = arr.length; //驗(yàn)證碼的長(zhǎng)度 var canvasStr,value; //驗(yàn)證碼值 function createCanvas(){ canvasStr = ''; value = ''; for(var i =0 ;i < 6;i++){ var a = arr[Math.floor(Math.random()*len)]; canvasStr += a+' '; value += a; } //canvas var oCanvas = document.getElementById('canvasCaptcha'); var ctx = oCanvas.getContext('2d'); var w = oCanvas.width; var h = oCanvas.height; var oImg = new Image(); oImg.src = './images/bg.jpg'; oImg.onload = function(){ var pattern = ctx.createPattern(oImg,'repeat'); ctx.fillStyle = pattern; ctx.fillRect(0,0,w,h); ctx.fillStyle = '#ccc'; ctx.textAlign = 'center'; ctx.font = '46px Roboto Slab'; ctx.setTransform(1,-0.12,0.2,1,0,12); ctx.fillText(canvasStr,w/2,60); } } createCanvas(); //驗(yàn)證輸入的驗(yàn)證碼與圖中驗(yàn)證碼時(shí)候相等 function captcha(e){ if(e == value){ return true; } else{ return false; } } //點(diǎn)擊提交按鈕時(shí)的結(jié)果 function showResult(){ var inputValue = $('.inputCaptcha').val(); if(inputValue == '' ||inputValue == null || inputValue == 'undefined'){ $('.errorText').css({display:'inline-block'}).html('驗(yàn)證碼不能為空,請(qǐng)重新輸入!'); $('.captchaIcon').css({display:'inline-block',backgroundImage:"url('./images/false.png')"}); }else{ var flag = captcha(inputValue); if(!flag){ $('.errorText').css({display:'inline-block'}).html('驗(yàn)證碼錯(cuò)誤,請(qǐng)重新輸入!'); $('.captchaIcon').css({display:'inline-block',backgroundImage:"url('./images/false.png')"}); }else{ $('.captchaIcon').css({display:'inline-block',backgroundImage:"url('./images/true.png')"}); } } } $('.captchaSubmit').click(showResult);//提交 $('.refresh').click(createCanvas);//刷新 //點(diǎn)擊input框 $('.inputCaptcha').focus(function(){ $('.errorText').add($('.captchaIcon')).fadeOut(100); })
以上是“js+canvas如何實(shí)現(xiàn)驗(yàn)證碼功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前文章:js+canvas如何實(shí)現(xiàn)驗(yàn)證碼功能
本文網(wǎng)址:http://m.newbst.com/article6/gdedog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、微信小程序、網(wǎng)站設(shè)計(jì)、域名注冊(cè)、網(wǎng)站設(shè)計(jì)公司、微信公眾號(hào)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)