vue+elementUI实现8个输入框对应手机验证码效果实现

579 阅读1分钟
<script>
export default {
  data () {
    return {
      textTitle: '验证手机号',
      phone: '154xxxxxxxx',
      timeTrue: true,
      time: 60,
      form: {
        f0: '',
        f1: '',
        f2: '',
        f3: '',
        f4: '',
        f5: '',
        f6: '',
        f7: '',
      }
    }
  },
  mounted() {
    this.countDown()
    this.$refs['f0'].focus() //第一个输入框获取焦点
  },
  watch: {
    //监听输入验证码变化,自动将焦点定位在第一个没有输入的输入框
    form: {
      handler: function(newval, oldval) {
        console.log(newval)
        for(let key in newval) {
          if(newval[key] == '') {
            this.$refs[key].focus()
            break;
          }
        }
      },
      deep: true
    }
  },
  methods: {
    //验证码倒计时计时
    countDown() {
      this.timeTrue = false;
      this.time = 60;
      var setTimeoutS = setInterval(() => {
        this.time--;
        if (this.time <= 0) {
          clearInterval(setTimeoutS);
          this.timeTrue = true;
        }
      }, 1000);
    },
    // 删除验证码操作
    onKeydown(e, i) {
      if (e.target.value === '' && e.key === 'Backspace') {
        // 删除上一个input里的值,并对其focus。
        if(i > 0) {
          this.form['f' + i] = '';
          this.$refs['f' + (i - 1)].focus();
        }
      }
    },
    //下一步
    onSubmit() {
    }
  },
}
</script>

<template>
  <div class="container">
    <div class="text-title">{{textTitle}}</div>
    <div class="text-info-verify">
      <div class="verify-content">
        <div class="notice">请输入发送至 <span class="phone">{{ phone }}</span> 的 8 位数验证码,有效期 5 分钟</div>
        <el-form ref="form" :model="form" inline>
          <el-form-item class="inline-num">
            <el-input v-model="form.f0" ref="f0" :class="form.f0 != '' ? 'has-num' : ''" @keydown.native="e=>{onKeydown(e, 0)}" oninput="if(isNaN(value)) { value = null }" maxlength="1"></el-input>
          </el-form-item>
          <el-form-item class="inline-num">
            <el-input v-model="form.f1" ref="f1" :class="form.f1 != '' ? 'has-num' : ''" @keydown.native="e=>{onKeydown(e, 1)}" oninput="if(isNaN(value)) { value = null }" maxlength="1"></el-input>
          </el-form-item>
          <el-form-item class="inline-num">
            <el-input v-model="form.f2" ref="f2" :class="form.f2 != '' ? 'has-num' : ''" @keydown.native="e=>{onKeydown(e, 2)}" oninput="if(isNaN(value)) { value = null }" maxlength="1"></el-input>
          </el-form-item>
          <el-form-item class="inline-num">
            <el-input v-model="form.f3" ref="f3" :class="form.f3 != '' ? 'has-num' : ''" @keydown.native="e=>{onKeydown(e, 3)}" oninput="if(isNaN(value)) { value = null }" maxlength="1"></el-input>
          </el-form-item>
          <el-form-item class="inline-num">
            <el-input v-model="form.f4" ref="f4" :class="form.f4 != '' ? 'has-num' : ''" @keydown.native="e=>{onKeydown(e, 4)}" oninput="if(isNaN(value)) { value = null }" maxlength="1"></el-input>
          </el-form-item>
          <el-form-item class="inline-num">
            <el-input v-model="form.f5" ref="f5" :class="form.f5 != '' ? 'has-num' : ''" @keydown.native="e=>{onKeydown(e, 5)}" oninput="if(isNaN(value)) { value = null }" maxlength="1"></el-input>
          </el-form-item>
          <el-form-item class="inline-num">
            <el-input v-model="form.f6" ref="f6" :class="form.f6 != '' ? 'has-num' : ''" @keydown.native="e=>{onKeydown(e, 6)}" oninput="if(isNaN(value)) { value = null }" maxlength="1"></el-input>
          </el-form-item>
          <el-form-item class="inline-num">
            <el-input v-model="form.f7" ref="f7" :class="form.f7 != '' ? 'has-num' : ''" @keydown.native="e=>{onKeydown(e, 7)}" oninput="if(isNaN(value)) { value = null }" maxlength="1"></el-input>
          </el-form-item>
          <div style="clear: both;"></div>
        </el-form>
        <div class="notice" v-show="!timeTrue">{{ time }} 秒后可重新获取验证码</div>
        <div class="get-code" v-show="timeTrue" @click="countDown">重新获取验证码</div>
      </div>
      <el-button class="btn" type="primary" @click="onSubmit">下一步</el-button>
    </div>
  </div>
</template>

<style lang="less" scoped>
.text-info-verify {
  width: 452px;
  margin: 0 auto;
  margin-top: 60px;
  font-size: 14px;
  text-align: center;
  .verify-content {
    text-align: left;
    .notice {
      color: #768893;
      font-size: 14px;
      .phone {
        font-size: 14px;
        font-weight: 500;
        color: #1d1d1d;
      }
    }
    .get-code {
      color: #4598f0;
    }
    .inline-num {
      float: left;
      width: 40px;
      margin: 24px 12px 16px 0;
      &:nth-child(4) {
        margin-right: 32px;
      }
    }
  }
  .btn {
    width: 200px;
    height: 34px;
    padding: 8px;
    margin-top: 60px;
  }
}

</style>
<style lang="less">
.verify-content {
  .el-input__inner {
    width: 40px;
    height: 40px;
    line-height: 40px;
    padding: 7px;
    font-size: 18px;
    text-align: center;
    color: #000;
  }
  .inline-num {
    &:nth-child(4) {
      .el-input::after {
        content: "";
        width: 8px;
        height: 1px;
        background: #ADB8BE;
        position: absolute;
        top: 20px;
        left: 52px;
      }
    }
    .has-num .el-input__inner{
      border: 1px solid #4598f0;
    }
  }
}
</style>