JS+CSS3制作图形验证码

一个前端验证的验证码,利用JS和CSS3实现。

网站登陆或会员注册页面要求网站能防止机器人功能(需要输入验证码,从而区别是机器还是人在操作)。

今天给大家分享的是一个前端验证的验证码。利用JS和CSS3的transform属性里的rotate设置旋转进行制作。

js+CSS3制作图形验证码

HTML页面代码:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>分享:js+CSS3制作图形验证码|www.xin126.cn</title>
    <script src="vCode.js"></script>
    <script>
        onload = function () {
            var container1 = document.getElementById("vCode1");
            var code1 = new vCode(container1);
            document.getElementById("btn1").addEventListener("click", function () {
                alert(code1.verify(document.getElementById("code1").value));
            }, false);
            var container2 = document.getElementById("vCode2");
            var code2 = new vCode(container2, {
                len: 5,
                bgColor: "#444444",
                colors: [
                    "#DDDDDD",
                    "#DDFF77",
                    "#77DDFF",
                    "#99BBFF",
                    //"#7700BB",
                    "#EEEE00"
                ]
            });
            document.getElementById("btn2").addEventListener("click", function () {
                alert(code2.verify(document.getElementById("code2").value));
            }, false);
        };
    </script>
</head>
<body>
<div>
    <h1>验证码1</h1>
    <input type="text" id="code1"/>
    <div id="vCode1" style="width:100px; height: 30px; border: 1px solid #ccc; display: inline-block;"></div>
    <button id="btn1">验证</button>
</div>
<div style="margin-top: 50px;">
    <h1>验证码2</h1>
    <input type="text" id="code2"/>
    <div id="vCode2" style="width:100px; height: 30px; border: 1px solid #ccc; display: inline-block;"></div>
    <button id="btn2">验证</button>
</div>
</body>
</html>

JS代码:

(function(){
    var randstr = function(length){
        var key = {
 
            str : [
                'a','b','c','d','e','f','g','h','i','j','k','l','m',
                'o','p','q','r','s','t','x','u','v','y','z','w','n',
                '0','1','2','3','4','5','6','7','8','9'
            ],
 
            randint : function(n,m){
                var c = m-n+1;
                var num = Math.random() * c + n;
                return  Math.floor(num);
            },
 
            randStr : function(){
                var _this = this;
                var leng = _this.str.length - 1;
                var randkey = _this.randint(0, leng);
                return _this.str[randkey];
            },
 
            create : function(len){
                var _this = this;
                var l = len || 10;
                var str = '';
 
                for(var i = 0 ; i<l ; i++){
                    str += _this.randStr();
                }
 
                return str;
            }
 
        };
 
        length = length ? length : 10;
 
        return key.create(length);
    };
 
    var randint = function(n,m){
        var c = m-n+1;
        var num = Math.random() * c + n;
        return  Math.floor(num);
    };
 
    var vCode = function(dom, options){
        this.codeDoms = [];
        this.lineDoms = [];
        this.initOptions(options);
        this.dom = dom;
        this.init();
        this.addEvent();
        this.update();
        this.mask();
    };
 
    vCode.prototype.init = function(){
        this.dom.style.position = "relative";
        this.dom.style.overflow = "hidden";
        this.dom.style.cursor = "pointer";
        this.dom.title = "点击更换验证码";
        this.dom.style.background = this.options.bgColor;
        this.w = this.dom.clientWidth;
        this.h = this.dom.clientHeight;
        this.uW = this.w / this.options.len;
    };
 
    vCode.prototype.mask = function(){
        var dom = document.createElement("div");
        dom.style.cssText = [
            "width: 100%",
            "height: 100%",
            "left: 0",
            "top: 0",
            "position: absolute",
            "cursor: pointer",
            "z-index: 9999999"
        ].join(";");
 
        dom.title = "点击更换验证码";
 
        this.dom.appendChild(dom);
    };
 
    vCode.prototype.addEvent = function(){
        var _this = this;
        _this.dom.addEventListener("click", function(){
            _this.update.call(_this);
        });
    };
 
    vCode.prototype.initOptions = function(options){
 
        var f = function(){
            this.len = 4;
            this.fontSizeMin = 20;
            this.fontSizeMax = 48;
            this.colors = [
                "green",
                "red",
                "blue",
                "#53da33",
                "#AA0000",
                "#FFBB00"
            ];
            this.bgColor = "#FFF";
            this.fonts = [
                "Times New Roman",
                "Georgia",
                "Serif",
                "sans-serif",
                "arial",
                "tahoma",
                "Hiragino Sans GB"
            ];
            this.lines = 8;
            this.lineColors = [
                "#888888",
                "#FF7744",
                "#888800",
                "#008888"
            ];
 
            this.lineHeightMin = 1;
            this.lineHeightMax = 3;
            this.lineWidthMin = 1;
            this.lineWidthMax = 60;
        };
 
        this.options = new f();
 
        if(typeof options === "object"){
            for(i in options){
                this.options[i] = options[i];
            }
        }
    };
 
    vCode.prototype.update = function(){
        for(var i=0; i<this.codeDoms.length; i++){
            this.dom.removeChild(this.codeDoms[i]);
        }
        for(var i=0; i<this.lineDoms.length; i++){
            this.dom.removeChild(this.lineDoms[i]);
        }
        this.createCode();
        this.draw();
    };
 
    vCode.prototype.createCode = function(){
        this.code = randstr(this.options.len);
    };
 
    vCode.prototype.verify = function(code){
        return this.code === code;
    };
 
    vCode.prototype.draw = function(){
        this.codeDoms = [];
        for(var i=0; i<this.code.length; i++){
            this.codeDoms.push(this.drawCode(this.code[i], i));
        }
 
        this.drawLines();
    };
 
    vCode.prototype.drawCode = function(code, index){
        var dom = document.createElement("span");
 
        dom.style.cssText = [
            "font-size:" + randint(this.options.fontSizeMin, this.options.fontSizeMax) + "px",
            "color:" + this.options.colors[randint(0,  this.options.colors.length - 1)],
            "position: absolute",
            "left:" + randint(this.uW * index, this.uW * index + this.uW - 10) + "px",
            "top:" + randint(0, this.h - 30) + "px",
            "transform:rotate(" + randint(-30, 30) + "deg)",
            "-ms-transform:rotate(" + randint(-30, 30) + "deg)",
            "-moz-transform:rotate(" + randint(-30, 30) + "deg)",
            "-webkit-transform:rotate(" + randint(-30, 30) + "deg)",
            "-o-transform:rotate(" + randint(-30, 30) + "deg)",
            "font-family:" + this.options.fonts[randint(0, this.options.fonts.length - 1)],
            "font-weight:" + randint(400, 900)
        ].join(";");
 
        dom.innerHTML = code;
        this.dom.appendChild(dom);
 
        return dom;
    };
 
    vCode.prototype.drawLines = function(){
        this.lineDoms = [];
        for(var i=0; i<this.options.lines; i++){
            var dom = document.createElement("div");
 
            dom.style.cssText = [
                "position: absolute",
                "opacity: " + randint(3, 8) / 10,
                "width:" + randint(this.options.lineWidthMin, this.options.lineWidthMax) + "px",
                "height:" + randint(this.options.lineHeightMin, this.options.lineHeightMax) + "px",
                "background: " + this.options.lineColors[randint(0, this.options.lineColors.length - 1)],
                "left:" + randint(0, this.w - 20) + "px",
                "top:" + randint(0, this.h) + "px",
                "transform:rotate(" + randint(-30, 30) + "deg)",
                "-ms-transform:rotate(" + randint(-30, 30) + "deg)",
                "-moz-transform:rotate(" + randint(-30, 30) + "deg)",
                "-webkit-transform:rotate(" + randint(-30, 30) + "deg)",
                "-o-transform:rotate(" + randint(-30, 30) + "deg)",
                "font-family:" + this.options.fonts[randint(0, this.options.fonts.length - 1)],
                "font-weight:" + randint(400, 900)
            ].join(";");
            this.dom.appendChild(dom);
 
            this.lineDoms.push(dom);
        }
    };
 
    this.vCode = vCode;
 
}).call(this);

125jz网原创文章。发布者:江山如画,转载请注明出处:http://www.125jz.com/11181.html

(0)
江山如画的头像江山如画管理团队
上一篇 2022年12月1日 下午1:48
下一篇 2022年12月1日 下午1:57

99%的人还看了以下文章

  • 简!修改Jupyter 默认打开目录的方法

    1、启动cmd,执行以下命令,查看 jupyter 配置文件路径 C:Users41588>jupyter notebook –generate-configWriting default config to: C:Users41588.jupyterjupyter_notebook_config.py 2、找到配置文件 jupyter_n…

    2023年7月19日 编程开发
    9.7K0
  • 纯JSP实现计算圆的面积和周长

    一个jsp页面由元素和模板数据组成.元素是必须由jsp容器处理的部分.而模板数据是jsp容器不处理的部分,如jsp中的HTML内容 元素有三种类型: 脚本元素,指令元素, 动作元素 脚本元素:包含三个部分:声明,脚本段,表达式 声明:用于声明在其它脚本元素中可以使用的变量和方法 脚本段:是一段java代码 表达式:java语言中完整的表达式 声明 以<…

    2020年4月3日
    21.0K0
  • 填坑!安装opencv-python库后,没有CV2文件夹,找不到haarcascade分类器文件

    python内import cv2正常运行,但是根据以下方法在e:\python\python39\lib\site-packages下找不到CV2文件夹,也找不到data\haarcascades相关分类器文件。 OpenCV-python haarcascade各种分类器文件位置 使用 pip list 查看是否安装opencv-python E:\py…

    2020年12月8日
    12.3K0
  • 国内github网站打不开的解决方法(验证100%解决)

    最近国内访问github.com经常打不开,无法访问。 github网站打不开的解决方法 1.打开网站http://tool.chinaz.com/dns/ ,在A类型的查询中输入 github.com,找出最快的IP地址。 2.修改hosts文件。 在hosts文件中添加: # localhost name resolution is handled wi…

    2022年2月1日 编程开发
    27.7K0
  • 单元测试工具JUnit介绍及使用,单元测试快速入门教程五

    JUnit是一个开放源代码的测试框架,用在编写和运行可重复的测试脚本之上,是用于Java语言编写的面向对象程序的单元测试工具。JUnit框架功能强大,目前已经成为Java单元测试的事实标准,基本上能满足日常的测试要求。 1.Junit主要特性 (1)可以将测试代码和产品代码分别开发,便于管理。 (2)针对某一个类的测试代码,以较少的改动便可以应用    另一…

    2018年4月18日 编程开发
    10.4K0
  • Myeclipse设置JSP视图和代码显示在同一个窗口

    Myeclipse开发时,想同时查看页面及代码视图,如上图,而默认只能看到代码。 Myeclipse设置JSP页面和代码显示在同一个窗口的方法有两种。 一、在JSP页面上右击-open with-other… 在弹出的Editor selection窗口中选择web page editor,就可以了。 二、选择 window-preferences,如下图,…

    2018年12月17日 编程开发
    5.2K0

发表回复

登录后才能评论