验证码组件

组件,依赖jquery

css

.code-input {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}
.code-input .code-container{
    display: flex;
    flex-wrap: nowrap;
}
.code-input .code-container .tb-code{
    box-sizing: border-box;
    border: solid 0.05rem #E7E8EB;
    border-right: 0;
    font-size: 0.9rem;
    text-align: center;
    background-color: transparent;
    color: #333333;
}
.code-input .code-container .tb-code:first-child{
    border-radius: 0.15rem 0 0 0.15rem;
}
.code-input .code-container .tb-code:last-child{
    border-radius: 0 0.15rem 0.15rem 0;
    border-right: solid 1px #E7E8EB;
}

js

;(function () {
    'use strict';
    function setAttributes(target, option) {
        Object.keys(option).forEach(function (key) {
            target.setAttribute(key, option[key])
        })
        return target
    }
    function createElement(tagName, option) {
        var dom = document.createElement(tagName)
        setAttributes(dom, option)
        return dom
    }
    function focusHandler(e) {
        this.input.focus();
    }
    function setVal(val) {
        var value = val.slice(0,this.codeLength) || ''
        for(var i=0;i<this.codeLength;i++){
            this.tabs[i].value = value[i] || '';
        }
        this.input.value = value
        this.currentVal = value
    }
    function inputHandler(e) {
        setVal.call(this, e.currentTarget.value)
    }
    function keyupHandler(e) {
        if(e.key === 'Enter' && this.currentVal.length===this.codeLength) {
            this.input.blur();
            if(Object.prototype.toString.call(this.option.oncomplete)==="[object Function]") {
                this.option.oncomplete.call(this, this.currentVal)
            }
        }else {
            return true
        }
    }
    function CodeInput(el, option){
        var target = null;
        if(el){
            target = el && el[0] ? el[0] : el
            if(target.nodeType !== 1){
                target = document.querySelector(target)
            }
        }
        if(!target){
            console.error('codeinput初始化失败,缺少初始化dom节点')
            return
        }
        this.currentVal = ''
        this.el = target
        this.option = option
        this.codeLength = option.codeLength || 6
        this.flag = true
        this.init()
    }
    CodeInput.prototype.init = function () {
        // 创建dom结构
        this.el.setAttribute('class',this.el.getAttribute('class') + ' code-input')
        this.input = createElement('input',
            {
                    id:'codeInput',
                    name: this.option.name || 'codeInput',
                    style: 'width: 100%;opacity: 0;height: 100%;position: absolute;top: 0;'
                }
            )
        this.tabsContainer = createElement('div',
            {
                id:'code-container',
                class: 'code-container'
            }
        )
        this.tabs = []
        for (var i=0; i<this.codeLength;i++){
            this.tabs[i] = createElement('input',
                {
                    id:'tb'+i,
                    disabled: 'disabled',
                    class: 'tb-code',
                    style: 'width:'+this.option.width+';height:'+this.option.height
                }
            )
            this.tabsContainer.append(this.tabs[i])
        }
        this.el.append(this.tabsContainer)
        this.el.append(this.input)
        this.addEventListener()
    }
    CodeInput.prototype.addEventListener = function () {
        var _self = this
        this.input.onkeyup = function (e) {
            keyupHandler.call(_self,e)
        }
        if(this.option.hasChinese) {
            $(this.input).on('compositionstart', function(){
                _self.flag = false;
            })
            $(this.input).on('compositionend', function(e) {
                _self.flag = true;
                inputHandler.call(_self, e)
            })
        }
        $(this.input).on('input', function (e) {
            if(_self.option.hasChinese){
                if(_self.flag){
                    inputHandler.call(_self, e)
                }
            }else {
                inputHandler.call(_self, e)
            }
        })
        this.tabsContainer.onclick = function (e) {
            focusHandler.call(_self,e)
        }
    }
    CodeInput.prototype.val = function (setValue) {
        if(setValue===undefined){
            return this.currentVal
        }
        setVal.call(this, setValue)
    }
    if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) {

        // AMD. Register as an anonymous module.
        define(function() {
            return CodeInput;
        });
    } else if (typeof module !== 'undefined' && module.exports) {
        module.exports = FastClick.attach;
        module.exports.FastClick = FastClick;
    } else {
        window.CodeInput = CodeInput;
    }
}())

使用方式

// 验证码位数
var repayCodeLength = 6,
var imageCode = $('#imageCode')
function oncomplete() {
    imageCode.focus()
}
repayCodeInstance = new CodeInput(repayCode,{
    width: '2.3775rem;',
    height: '2.2rem',
    name: 'repayCode',
    codeLength: repayCodeLength,
    hasChinese: false,
    oncomplete: oncomplete
})
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容