vue 验证码输入框

<template>
  <div class="classnumber">
    <div class="v-code">
      <input
        ref="vcode"
        id="vcode"
        type="tel"
        maxlength="codeLength"
        v-model="aCodes"
        @focus="focused = true"
        @blur="focused = false"
        :disabled="telDisabled"
        :placeholder="placeholder"
        :class="{'on':focused||aCodes}"
      />
      <label
        v-show="focused||aCodes"
        for="vcode"
        class="line"
        v-for="(item,index) in codeLength"
        :key="index"
        :class="{'animated': focused && cursorIndex === index}"
        v-text="codeArr[index]"
      ></label>
    </div>
  </div>
</template>
<script>
export default {
  props: {
    codeLength: {
      type: Number,
      default: 4
    },
    placeholder:{
      type:String,
      default:''
    }
  },
  data() {
    return {
      aCodes: "",
      telDisabled: false,
      focused: false
    };
  },
  model: {
    prop: "value", //绑定的值,通过父组件传递
    event: "update2" //自定义名 可以随便改
  },
  computed: {
    codeArr() {
      return this.aCodes.split("");
    },
    cursorIndex() {
      return this.aCodes.length;
    }
  },
  watch: {
    aCodes(newVal, oldVal) {
      if (newVal.length > this.codeLength||!Number(newVal)) {
        this.aCodes = oldVal;
        return false;
      }
      this.$emit("update2", newVal);
    }
  }
};
</script>
<style lang="scss">
.v-code {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  position: relative;
  width: 100%;
  margin-left: auto;
  margin-right: auto;
}

.v-code input {
  position: absolute;
  border-bottom: vh(1) solid $borderColor;
  color: #181818;
  height: 32px;
  line-height: 32px;
  outline: none;
  width: 100%;
  font-size: vh(22);
}
.v-code .on {
  opacity: 0;
}
.v-code .line {
  position: relative;
  width: 40px;
  height: 32px;
  line-height: 32px;
  text-align: center;
  font-size: vh(22);
}
.v-code .line::after {
  display: block;
  position: absolute;
  content: "";
  left: 0;
  width: 100%;
  bottom: 0;
  height: 1px;
  background-color: #aaaaaa;
  transform: scaleY(0.5);
  transform-origin: 0 100%;
}
.v-code .line.animated::before {
  display: block;
  position: absolute;
  left: 50%;
  top: 20%;
  width: 1px;
  height: 60%;
  content: "";
  background-color: #333333;
  animation-name: coruscate;
  animation-duration: 1s;
  animation-iteration-count: infinite;
  animation-fill-mode: both;
}

@keyframes coruscate {
  0% {
    opacity: 0;
  }
  25% {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  75% {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}
</style>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值