vue字母加数字组件

效果图:


组件代码:

<template>

        <div>

             <div class="head-label head-message" >输入名字:</div>

             <div style="display: inline-block;">

                 <input v-model="shownumber" type="text" ref="number" required="required" class="number show-number">

                 <img src="../Image/graycancel.png" class="img" @click="deletenumber()">

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="finish-01" :btnData="{text: '1',btnWidth:'93px',btnHeight:'96px'}" @click="showtext(1)"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="finish-02" :btnData="{text: '2',btnWidth:'93px',btnHeight:'96px'}"  @click="showtext(2)"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="finish-02" :btnData="{text: '3',btnWidth:'93px',btnHeight:'96px'}"  @click="showtext(3)"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="finish-02" :btnData="{text: '4',btnWidth:'93px',btnHeight:'96px'}"  @click="showtext(4)"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="finish-02" :btnData="{text: '5',btnWidth:'93px',btnHeight:'96px'}"  @click="showtext(5)"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="finish-02" :btnData="{text: '6',btnWidth:'93px',btnHeight:'96px'}" @click="showtext(6)"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="finish-02" :btnData="{text: '7',btnWidth:'93px',btnHeight:'96px'}" @click="showtext(7)"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="finish-02" :btnData="{text: '8',btnWidth:'93px',btnHeight:'96px'}" @click="showtext(8)"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="finish-02" :btnData="{text: '9',btnWidth:'93px',btnHeight:'96px'}" @click="showtext(9)"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="finish-02" :btnData="{text: '0',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('0')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn-01" :btnData="{text: 'A',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('A')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'B',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('B')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'C',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('C')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'D',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('D')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'E',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('E')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'F',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('F')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'G',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('G')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'H',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('H')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'I',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('I')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'J',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('J')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn-01"  :btnData="{text: 'K',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('K')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'L',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('L')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'M',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('M')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'N',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('N')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'O',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('O')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'P',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('P')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'Q',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('Q')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'R',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('R')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'S',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('S')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'T',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('T')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn-01"  :btnData="{text: 'U',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('U')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'V',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('V')" ></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'W',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('W')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'X',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('X')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'Y',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('Y')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: 'Z',btnWidth:'93px',btnHeight:'96px'}" @click="showtext('Z')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: '空格',btnWidth:'198px',btnHeight:'96px'}" @click="showtext('空格')"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: '←',btnWidth:'93px',btnHeight:'96px'}" @click="changefront()"></DrawButton>

             </div>

             <div style="display: inline-block;">

                 <DrawButton class="secondcolumn"  :btnData="{text: '→',btnWidth:'93px',btnHeight:'96px '}" @click="changeafter()"></DrawButton>

             </div>

        </div>

</template>

<script>

import DrawButton from '../../../components/DrawButton'

export default {

    name:'DrawSecond',

    components: {

      DrawButton

    },

    data(){

        return{

            value: [],

            show:true,

            showall:true,

            shownumber:'',

            showfront:'',

            showafter:'',

            count:0

              }

    },

    watch:{

        'shownumber':function(newval){

            console.log('监听结果为'+newval)

            this.$emit('selectnumber',newval);

        }

    },

    methods:{

        selectnumber(){

            if(this.shownumber.length==0){

            this.$warning({

            title: '提示',

            content: '证件号码不能为空',

            okText:'确定'

                });

            }else{

            this.$emit('selectnumber',this.shownumber);

            this.$emit('hide');

            }

        },

        hidedraw(){

            this.$emit('hide');

        },

        deletenumber(){

            console.log(this.shownumber)

            let front='';

            let after='';

            let doc = this.$refs['number'];//获取光标位置  

            console.log(doc.selectionStart)

            doc.setSelectionRange(this.count,this.count);

            front=this.shownumber.substring(0,doc.selectionStart);

            after=this.shownumber.substring(doc.selectionStart);

            front = front.substr(0,front.length-1);

            console.log(front)

            console.log(after)

            this.shownumber = front + after;

            // this.shownumber = this.shownumber.substr(0,this.shownumber.length-1);

            console.log(this.shownumber);

        },

        showtext(text){           

            let doc = this.$refs['number'];//获取光标位置   

            doc.setSelectionRange(this.count,this.count);

            console.log('count='+this.count)      

            this.showfront=this.shownumber.substring(0,doc.selectionStart);

            this.count = doc.selectionStart+1;

            console.log('count='+this.count)  

            this.showafter=this.shownumber.substring(doc.selectionStart);

            if(text == '空格'){

                this.showfront+=' ';               

            }

            else{

                this.showfront+=text

            }

            console.log('组合值='+this.showfront);

            console.log('组合值='+this.showafter);

            this.shownumber = this.showfront+this.showafter;

        },

        changefront(){

             this.$refs.number.focus();

             let doc = this.$refs['number'];

             doc.setSelectionRange(doc.selectionStart-1,doc.selectionStart-1);

             this.count = doc.selectionStart;

        },

        changeafter(){

             this.$refs.number.focus();

             let doc = this.$refs['number'];

             doc.setSelectionRange(doc.selectionStart+1,doc.selectionStart+1);   

             this.count = doc.selectionStart;        

        }

    }

}

</script>

<style>

.img{

background-size: 60px 44px;

background-repeat: no-repeat;

margin-top: -18px;

margin-left: -59px;

}

.number{

width: 835px;

height: 65px;

border-radius: 4px;

background-color: rgba(222, 222, 222, 1);

/* border: 1px solid rgba(85, 85, 85, 1); */

}

.head-message{

font-size: 30px;

text-align: left;

font-family: STHeitiSC-Medium;

}

.head-label{

margin-left: 56px;

margin-top: 4px !important;

width: 150px;

height: 98px;

line-height: 98px;

display: inline-block;

color: rgba(51, 51, 51, 1);

}

.show-number{

color: rgba(51, 51, 51, 1);

font-size: 30px;

text-align: left;

font-family: STHeitiSC-Medium;

}

.finish{

    margin-left: 13px;

}

.finish-01{

    margin-left: 36px;

    margin-top: 15px;

}

.finish-02{

    margin-left: 11px;

    margin-top: 15px;

}

.secondcolumn-01{

    margin-left: 36px;

    margin-top: 15px;

}

.secondcolumn{

    margin-left: 11px;

    margin-top: 15px;

}

.head-button{

width: 106px;

height: 67px;

border-radius: 4px;

background:linear-gradient(to bottom,#646467,#4D4E53);

background-color: #ffffff;

opacity: 1; 

box-shadow: inset 0px 0px 0px 0px rgba(255, 255, 255, 0.5);

}

.head-button-message{

width: 60px;

height: 30px;

color: rgba(223, 223, 223, 1);

}

.body-message{

width: 35px;

height: 36px;

color: rgba(228, 228, 228, 1);

font-size: 36px;

text-align: center;

font-family: STHeitiSC-Medium;

}

</style>



需要的另一个组件代码:

<template>

    <!-- use plane  -->

    <!-- 传入bgColor改变按钮背景色 -->

    <!-- IsDisabled切换button的状态 -->

    <!-- text为按钮文字 -->

    <div>

        <div class="DrawButton" :style="{width:btnWidth,height:btnHeight,lineHeight:btnHeight}"  @click="click(text)">

            {{text}}

        </div>

    </div>

</template>

<script>

export default {

    name: "DrawButton",

        data(){

            return {

                //按钮文本

                text: this.btnData.text,

                btnWidth: this.btnData.btnWidth,

                btnHeight: this.btnData.btnHeight

            }

        },

        props: {

            btnData: {

                types: Array,

                default() {

                    return {

                        text: '',

                        btnWidth:'',

                        btnHeight:''

                    }

                }

            }

        },

        methods: {

            click(text){

                this.IsDisabled = false;

                //这里是激活父组件的事件,因为子组件是不会冒泡到父组件上的,必须手动调用$emit

                //相对应父组件要在调用该组件的时候,将其挂载到上面

                this.$emit("click")

            }

        }

}

</script>

<style lang="less">

    .DrawButton {

        text-align: center;

        color: rgba(51, 51, 51, 1);

        font-family: STHeitiSC-Medium;

        border-radius: 4px;

        opacity: 1; 

        letter-spacing: 1px;

        font-size: 36px;

        letter-spacing: 2px;

        background-color: rgba(255, 255, 255, 1);

        box-shadow: 0px 2px 0px 0px rgba(135, 138, 145, 1);

    }

</style>

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,496评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,407评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,632评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,180评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,198评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,165评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,052评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,910评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,324评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,542评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,711评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,424评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,017评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,668评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,823评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,722评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,611评论 2 353