欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js+canvas實現(xiàn)驗證碼功能

 更新時間:2020年09月21日 15:43:33   作者:灑-脫  
這篇文章主要為大家詳細介紹了js+canvas實現(xiàn)驗證碼功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

剛剛開始接觸canvas,寫個驗證碼小功能練練手,實現(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 = "請輸入驗證碼">
  <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));
}
//每個驗證碼可能出現(xiàn)的字母或數(shù)字(區(qū)分大小寫)
var len = arr.length;
//驗證碼的長度
var canvasStr,value;
//驗證碼值
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();

//驗證輸入的驗證碼與圖中驗證碼時候相等
function captcha(e){
 if(e == value){
 return true;
 }
 else{
 return false;
 }
}

//點擊提交按鈕時的結(jié)果
function showResult(){
 var inputValue = $('.inputCaptcha').val();
 
 if(inputValue == '' ||inputValue == null || inputValue == 'undefined'){
 $('.errorText').css({display:'inline-block'}).html('驗證碼不能為空,請重新輸入!');
 $('.captchaIcon').css({display:'inline-block',backgroundImage:"url('./images/false.png')"});
 }else{
 var flag = captcha(inputValue);
 if(!flag){
  $('.errorText').css({display:'inline-block'}).html('驗證碼錯誤,請重新輸入!');
  $('.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);//刷新
//點擊input框
$('.inputCaptcha').focus(function(){
 $('.errorText').add($('.captchaIcon')).fadeOut(100);
})

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • JS項目中對本地存儲進行二次的封裝的實現(xiàn)

    JS項目中對本地存儲進行二次的封裝的實現(xiàn)

    這篇文章主要介紹了JS項目中對本地存儲進行二次的封裝,這里我們將要使用到的key存儲下來,新建一個叫constant-storage.js的文件,對外暴露一些key的鍵名,也方便后期統(tǒng)一修改,這里因為都是恒量,所以名稱我們都用大寫表示,需要的朋友可以參考下
    2022-07-07
  • js實現(xiàn)簡單的秒表

    js實現(xiàn)簡單的秒表

    這篇文章主要為大家詳細介紹了js實現(xiàn)簡單的秒表,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-01-01
  • layui2.0使用table+laypage實現(xiàn)真分頁

    layui2.0使用table+laypage實現(xiàn)真分頁

    這篇文章主要為大家詳細介紹了layui2.0使用table+laypage實現(xiàn)真分頁,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • JS實現(xiàn)瀏覽器菜單命令

    JS實現(xiàn)瀏覽器菜單命令

    JS實現(xiàn)瀏覽器菜單命令...
    2006-09-09
  • TypeScript實現(xiàn)數(shù)組和樹的相互轉(zhuǎn)換

    TypeScript實現(xiàn)數(shù)組和樹的相互轉(zhuǎn)換

    樹或者圖是個比較抽象的概念,并不存在這樣的數(shù)據(jù)類型。數(shù)組就比較簡單了,因此數(shù)組和樹的轉(zhuǎn)換可以理解為數(shù)組和對象之間的轉(zhuǎn)換。本文將用TypeScript實現(xiàn)數(shù)組和樹的相互轉(zhuǎn)換,感興趣的可以了解一下
    2022-06-06
  • js實現(xiàn)網(wǎng)頁版貪吃蛇游戲

    js實現(xiàn)網(wǎng)頁版貪吃蛇游戲

    這篇文章主要為大家詳細介紹了js實現(xiàn)網(wǎng)頁版貪吃蛇游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • 原生js更改css樣式的兩種方式

    原生js更改css樣式的兩種方式

    本文主要介紹了原生js更改css樣式的兩種方式,具有很好的參考價值,下面跟著小編一起來看下吧
    2017-03-03
  • JavaScript+Java實現(xiàn)HTML頁面轉(zhuǎn)為PDF文件保存的方法

    JavaScript+Java實現(xiàn)HTML頁面轉(zhuǎn)為PDF文件保存的方法

    借助iText這個Java庫,我們可以將HTML文件保存為圖片文件進而轉(zhuǎn)換成PDF格式,接下來就來具體看下JavaScript+Java實現(xiàn)HTML頁面轉(zhuǎn)為PDF文件保存的方法
    2016-05-05
  • 詳解ES6 系列之異步處理實戰(zhàn)

    詳解ES6 系列之異步處理實戰(zhàn)

    這篇文章主要介紹了詳解ES6 系列之異步處理實戰(zhàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-10-10
  • js判斷瀏覽器是否支持html5

    js判斷瀏覽器是否支持html5

    這篇文章主要介紹了如何使用js判斷瀏覽器是否支持html5,需要的朋友可以參考下
    2014-08-08

最新評論