1.verification-code.js

;(function(){
var data = [];//验证码数据
function picVerificationCode(config){/*验证码:数字和字母组成@param config {Object} 配置参数@param config.container {DOM元素} 验证码显示容器 必填@param config.num {Number} 验证码个数 非必填@param config.iconum {Number} 背景斑点个数 非必填@param config.changeBtn {DOM元素数组} 点击刷新验证码 非必填@param config.strFontSize {Array} 验证码字符大小 非必填@param config.width {Number} 验证码显示容器宽度 非必填@param config.height {Number} 验证码显示容器高度 非必填@param config.background {String} 验证码显示容器背景颜色 非必填@param config.cimgRefresh {Boolean} 是否点击验证码图片后刷新验证码 非必填*///参数判断if(dataType(config)!='Object'){console.log('参数必须是object')return}if(!config.container||!isElement(config.container)){console.log('container参数必须是Dom元素')return;}//参数初始化(默认值)var defalutConfig = {num:4,iconum:10,width:160,height:40,background:'#ddd',fsize:[18,20,22,16],cimgRefresh:true}var config = Object.assign({},defalutConfig,config);//合并参数var verificationCode=[0,1,2,3,4,5,6,7,8,9,'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','Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M'];var tlist = [],//验证码字符数组color=[],//字符颜色rote=[],//字符旋转角度fsize=[];//字符字体大小config.container.innerHTML = '';//初始化验证码容器for(var i = 0;i<config.num;i++){var a = '';for(var j=0;j<3;j++){a+=randomNum(255).toString(16)}if(a.length == 4||a.length == 5){a = a.substr(0,3)}color.push('#'+a)if(config.fsize[i]){fsize.push(config.fsize[i]+'px')}else{fsize.push(config.fsize[randomNum(0,config.fsize.length-1)]+'px')   }tlist.push(verificationCode[randomNum(61)]);rote.push(randomNum(-45,45));}//设置验证码容器样式var box = config.containerbox.style.background = config.background;box.style.width = config.width+'px';box.style.height = config.height+'px';box.style.lineHeight = config.height+'px';box.style.display = 'inline-block';box.style.position = 'relative';box.style.textAlign = 'center';box.style.padding = 0;box.style.userSelect = 'none';var boxWidth = box.offsetWidth;var boxHeight = box.offsetHeight;var IconColor=[];//斑点颜色for(var m=0;m<config.iconum;m++){var threeColor = '';for(var j=0;j<3;j++){threeColor +=randomNum(255).toString(16)}if(threeColor.length == 4||threeColor.length == 5){threeColor = threeColor.substr(0,3)}IconColor.push('#'+threeColor)}for(var n=0;n<config.iconum;n++){var eltag = document.createElement("div");eltag.style.background = IconColor[n];eltag.style.position = 'absolute';eltag.style.width = '3px';eltag.style.height = '3px';eltag.style.borderRadius = '50%';eltag.style.top = randomNum(boxHeight-2)+'px';eltag.style.left = randomNum(boxWidth-2)+'px'box.appendChild(eltag)//添加斑点}for(var k=0;k<config.num;k++){//添加验证码字符var span = document.createElement("span");span.innerHTML=tlist[k];//内容span.style.color=color[k];//颜色span.style.fontSize=fsize[k];//字体大小span.style.display = 'inline-block';span.style.width = Math.floor((config.width/config.num))+'px';if(supportCss3('transform')){//浏览器如果支持css3 transform则设置span.style.transform='rotate('+rote[k]+'deg)';//旋转角度}box.appendChild(span)}//点击刷新验证码if(config.cimgRefresh){config.container.addEventListener('click',function(){picVerificationCode(Object.assign({},config,{cimgRefresh:false,changeBtn:null}))},true)}if(dataType(config.changeBtn)=='Array'){config.changeBtn.forEach(function(item,index){if(!isElement(item))return;item.addEventListener('click',function(){picVerificationCode(Object.assign({},config,{cimgRefresh:false,changeBtn:null}))},true)})}config.container.setAttribute('data-verificode',tlist)data = tlist;return tlist;
}
/******获取验证码数据******/
function getData(){return data
}
/******数组乱序******/
function shuffle(arr) {var length = arr.length,randomIndex,temp;while (length) {randomIndex = Math.floor(Math.random() * (length--));temp = arr[randomIndex];arr[randomIndex] = arr[length];arr[length] = temp}return arr;
}
/******判断浏览器是否支持某个css属性*******/
function supportCss3(style) {var prefix = ['webkit', 'Moz', 'ms', 'o'],i,humpString = [],htmlStyle = document.documentElement.style,_toHumb = function (string) {return string.replace(/-(\w)/g, function ($0, $1) {return $1.toUpperCase();});};for (i in prefix)humpString.push(_toHumb(prefix[i] + '-' + style));humpString.push(_toHumb(style));for (i in humpString)if (humpString[i] in htmlStyle) return true;return false;
}
/******判断js变量是否是DOM元素*******/
function isElement(OBJ){return HTMLElement?OBJ instanceof HTMLElement:(OBJ && typeof OBJ === 'object' &&(OBJ.nodeType === 1||OBJ.nodeType === 9) && typeof OBJ.nodeName === 'string')
}
/******判断数据类型*******/
function dataType(data){if(data === null) return 'Null';if(data === undefined) return 'Undefined';return Object.prototype.toString.call(data).slice(8,-1);}
/******获取[a,b]范围内的随机数******/
function randomNum(minNum,maxNum){ switch(arguments.length){ case 1: return parseInt(Math.random()*minNum+1,10); break; case 2: return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10); break; default: return 0; break; }
}
window.verifiCode = {picVerificationCode:picVerificationCode,getData:getData,shuffle:shuffle,supportCss3:supportCss3,isElement:isElement,dataType:dataType,randomNum:randomNum}
})();

2.使用

<!DOCTYPE html>
<html>
<head><title></title><script src='./verification-code.js'></script>
</head>
<body><div id='picCode'></div><button id='refresh'>刷新验证码</button><button id='getCode'>获取验证码</button>
<script>
var ww = verifiCode.picVerificationCode({num:5,//验证码个数iconum:20,//背景斑点个数fsize:[18,20,22,16],container:document.getElementById('picCode'),changeBtn:[document.getElementById('refresh')]})
document.getElementById('getCode').addEventListener('click',function(){var data = verifiCode.getData()console.log(data)
},true)
</script>
</body>
</html>

3.效果图

前端实现图片验证码效果(数字和字母)相关推荐

  1. 图像 - 识别出图片里的数字和字母

    本文给大家分享的是C#识别出图片里的数字和字母的代码,主要是识别以前公司的软件注册码截图里的数字和字母,功能很简单,也存在很大的局限性,这里仅仅是分享,小伙伴们参考下. 一个图片识别小工具,原先主要是 ...

  2. php实现验证码(数字、字母、汉字)

    验证码在表单实现越来越多了,但是用js的写的验证码,总觉得不方便,所以学习了下php实现的验证码.好吧,其实是没有事情干,但是又不想浪费时间,所以学习了下php实现验证码.正所谓,技多不压身.而且,也 ...

  3. python随机生成验证码,数字+大小写字母

    ASCII码的对照链接 大写字母的十进制范围是(65,91) 小写字母的十进制范围是(97,123) 数字的十进制范围是(48,58) 思路: 1.先在空链表中添加大小写字母和数字 2.从列表中随机选 ...

  4. 前端将后端返回的数字(英文字母),转成对应的文字进行显示

    前言 类似于省市区,状态,性别之类的可以枚举出来的变量,一般情况下数据库中存储的是其对应的value值,像省市区联动的话,前端界面中显示的是省市区的名字,但是接口的入参一般是对应的code码:如果界面 ...

  5. 随机验证码(数字和字母组成)及toLowerCase() 字符串转小写方法和toUpperCase()字符串转大写方法...

    JavaScript toLowerCase() 方法和toUpperCase()方法 toLowerCase()方法 定义:toLowerCase() 方法用于把字符串转换为小写. 语法:var s ...

  6. 前端输入框限制只能输入数字和字母

    const addressReg = /^[a-zA-Z0-9_]$/const address = valueif (!addressReg.test(address)) {this.formInl ...

  7. 随机数字图片验证码的原理、生成和破解

    http://sxdt.h.baike.com/article-1307487.html 随机数字图片验证码的原理.生成和破解 2013-10-21 10:27:49 本文行家:jdsongss 随机 ...

  8. python识别图片中数字_Python图像处理之图片验证码识别

    在上一篇博客Python图像处理之图片文字识别(OCR)中我们介绍了在Python中如何利用Tesseract软件来识别图片中的英文与中文,本文将具体介绍如何在Python中利用Tesseract软件 ...

  9. Django之头像实时展示到input框、图片验证码、简单发邮件

    一.以注册功能来看头像实时展示 首先先来写一个简单的页面: <div class="container-fluid"><div class="row&q ...

最新文章

  1. R语言ggplot2可视化水平条形图的标题(title)、副标题(subtitle)和图片说明信息(caption)左对齐实战
  2. DIV+CSS圆角边框
  3. Ajax异步中文数据处理
  4. think php ajax分页,thinkPHP5框架实现基于ajax的分页功能示例
  5. Javascript 逗号“,”引发的血案
  6. sudo apt install ...
  7. JAVA使用ByteArrayOutputStream、ByteArrayInputStream将对象序列化反序列化,通过JAVA socket实现对象在网络中传输
  8. Lakehouse 架构解析与云上实践
  9. Public Sale【博弈】
  10. TensorFlow笔记(3) TensorBoard可视化
  11. 如何一个动态创建对象?
  12. 黑马程序员---java基础------------------基础中的基础学习
  13. C#用串口接收事件接不全数据的处理
  14. Hibdernate入门
  15. php 上传图片转base64格式,PHP实现本地图片转base64格式并上传
  16. free 显示系统内存的使用情况
  17. E71使用蓝牙连接PC套件的一些经验
  18. 解决打开WORD时提示的:“无法复制文件:无法读源文件或磁盘”
  19. 目前建站系统用的比较多的几个系统(几款值得推荐的建站系统)
  20. 广告代码(弹窗和富媒体)

热门文章

  1. r相机采集的图片转化为halcon识别的图像源码_直播系统开发:关于直播源码中美颜SDK的作用...
  2. 失落世界服务器国庆宝箱位置,失落的斯菲尔 图文攻略 全剧情任务流程全宝箱收集...
  3. 实习总结第七谈-----------vue中element-ui的表格行高怎么控制
  4. htmltabl+imgki发送到企业微信
  5. 「镁客·请讲」打造一台眼睛专属“跑步机”,鹰视菲诺是如何用AI拯救近视的?...
  6. 全球破解组织网址大全
  7. 夺宝答题王小程序完整源码(含前端/后端以及数据库脚本)
  8. [JAVA安全webshell]冰蝎jsp木马分析
  9. DOM Scripting 学习六 - Image Gallery
  10. 全 3D 社交网络 Beloola 测试视频