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)
江山如画的头像江山如画管理团队
9个要点让你成为优秀的Java程序员
上一篇 2022年12月1日 下午1:48
JSP页面使用Servlet制作图形验证码
下一篇 2022年12月1日 下午1:57

99%的人还看了以下文章

  • createStatement参数详解,带参数与不带参数的区别

    Connection接口的createStatement方法,可用来创建向数据库发送SQL语句的对象,用于执行SQL语句。 常用代码: Statement stmt = con.createStatement(); Statement stmt = con.createStatement (int resultSetType,int resultSetCon…

    2018年10月23日
    10.8K0
  • 单元测试经验总结,单元测试快速入门教程六

    测试人员在进行测试的工作过程中,应该注意积累测试工作经验,这样可以缩短单元测试的时间,提高测试效果和效率。 如: 1.在做单元测试的过程中,要灵活选用测试用例设计技术,可以首先使用黑盒测试用例设计技术,然后根据相应的覆盖率统计再补充白盒测试用例。这样既减少了测试工作的重复,又保证了单元测试的完整性。 2.设计驱动程序时,要保证测试逻辑的正确性。否则,即使代码…

    2018年4月18日
    4.2K0
  • 开发软件,编程语言Java和C++选哪个?

    根据网络调查数据:编程语言排名前三的是Java、C#、C++。 其中Java使用者比例最高,为42.82%,是C#的两倍还要多。 紧跟其后的是C#,比例为17.33%。 排名第三的C++则有14.35%的比例。 JAVA和C++都是面向对象语,都能够实现面向对象思想(封装,继乘,多态)。而由于C++为了照顾大量的C语言使用者,而兼容了C,使得自身仅仅成为了带…

    2021年2月16日
    7.7K0
  • jsp写mysql数据库出现中文乱码

    今天用jsp做个图片书管理系统,向mysql数据库中存中文的时候显示乱码,如图书名: web???? 修改页面是http://localhost:8080/library/book?action=bookModifyQuery&ID=14 根据中文乱码的处理方法: 表单method方式为post或get中文乱码的解决方法 jsp:include包含h…

    2020年8月22日
    5.2K0
  • object…object和object[]…object的区别

    object…object public int update(String sql, Object… params) throws SQLException { Connection conn = this.prepareConnection(); return this.update(conn, true, sql, params); }…

    2019年11月19日
    16.3K0
  • 一秒解决cv2.imshow(”, frame) size.width>0 && size.height>0 in function ‘cv::imshow’ 错误

    看网上对size.width>0 && size.height>0 in function ‘cv::imshow’ 错误多是提示,路径问题:要修改路径中的\为/,或要改为绝对路径。 又或是路径包含中文,改成全英文。 搜了半天,没有一个解决问题。 我们静下心来,看下错误提示size.width>0 &&…

    2022年9月12日
    14.1K0

发表回复

登录后才能评论