VUE every minute using a write input component codes

This article is reproduced in: Ape 2048 website ⇨ https://www.mk2048.com/blog/blog.php?id=hc0c1iiccb

effect

First look at FIG wave effect complete

Renderings

Preview Address

github address

npm address

demand

4 or 6-position input message authentication code, the keyboard input is complete Collapse

Implementation steps

first step

Layout layout

<div class="security-code-wrap">
    <label for="code">
      <ul class="security-code-container">
        <li class="field-wrap" v-for="(item, index) in number" :key="index">
          <i class="char-field">{{value[index] || placeholder}}</i>
        </li>
      </ul>
    </label>
    <input ref="input" class="input-code" @keyup="handleInput($event)" v-model="value"
           id="code" name="code" type="tel" :maxlength="number"
           autocorrect="off" autocomplete="off" autocapitalize="off">
</div>
  • Use li elements to simulate the input box is displayed, no other purpose, just to semantics, of course, you can also use any other element to simulate, for example, div.
  • The benefits of using label tag is that it can be associated with the click event input on the one hand to achieve a semantic solution, it also eliminates the need for us to evoke the virtual keyboard by js.

Hidden input box

.input-code {
    position: absolute;
    left: -9999px;
    top: -9999px;
}
  • The real input box positioned outside the visible area of ​​the screen where, when the virtual keyboard is aroused, will not be up top of the page. So your verification code input component must be placed where the virtual keyboard that can not be blocked.

The second step

Processing input codes

handleSubmit () {
  this.$emit('input', this.value)
},
handleInput (e) {
  if (e.target.value.length >= this.length) {
    this.hideKeyboard()
  }
  this.handleSubmit()
}
  • Input, to assign a value input box, in order to solve the input end of the rear frame android defocus refocusing the input cursor in front of the first set, after the assignment refocusing, position the cursor displayed on the last one Behind.

third step

Once completed close the virtual keyboard

hideKeyboard() {
  // 输入完成隐藏键盘
  document.activeElement.blur() // ios隐藏键盘
  this.$refs.input.blur() // android隐藏键盘
}

Components complete code

<template>
  <div class="security-code-wrap">
    <label :for="`code-${uuid}`">
      <ul :class="`${theme}-container security-code-container`">
        <li class="field-wrap" v-for="(item, index) in length" :key="index">
          <i class="char-field">{{value[index] || placeholder}}</i>
        </li>
      </ul>
    </label>
    <input ref="input" class="input-code" @keyup="handleInput($event)" v-model="value"
           :id="`code-${uuid}`" :name="`code-${uuid}`" type="tel" :maxlength="length"
           autocorrect="off" autocomplete="off" autocapitalize="off">
  </div>
</template>

<script>
  export default {
    name: 'SecurityCode',
    // component properties
    props: {
      length: {
        type: Number,
        default: 4
      },
      placeholder: {
        type: String,
        default: '-'
      },
      theme: {
        type: String,
        default: 'block'
      }
    },
    // variables
    data () {
      return {
        value: ''
      }
    },
    computed: {
      uuid () {
        return Math.random().toString(36).substring(3, 8)
      }
    },
    methods: {
      hideKeyboard () {
        // 输入完成隐藏键盘
        document.activeElement.blur() // ios隐藏键盘
        this.$refs.input.blur() // android隐藏键盘
      },
      handleSubmit () {
        this.$emit('input', this.value)
      },
      handleInput (e) {
        if (e.target.value.length >= this.length) {
          this.hideKeyboard()
        }
        this.handleSubmit()
      }
    }
  }
</script>

<style scoped lang="less">
  .security-code-wrap {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .security-code-container {
    margin: 0;
    padding: 0;
    display: flex;
    .field-wrap {
      list-style: none;
      display: block;
      height: 40px;
      width: 40px;
      line-height: 40px;
      font-size: 16px;
      .char-field {
        font-style: normal;
      }
    }
  }

  .block-container {
    justify-content: center;
    .field-wrap {
      background-color: #fff;
      margin: 2px;
      color: #000;
    }
  }

  .line-container {
    position: relative;
    background-color: #fff;
    &:after {
      box-sizing: border-box;
      content: "";
      width: 200%;
      height: 200%;
      transform: scale(.5);
      position: absolute;
      border: 1px solid #d9d9d9;
      top: 0;
      left: 0;
      transform-origin: 0 0;
      border-radius: 4px;
    }
    .field-wrap {
      flex: 1;
      position: relative;
      &:not(:last-child):after {
        content: "";
        width: 1px;
        height: 50%;
        position: absolute;
        right: 0;
        top: 25%;
        background-color: #d9d9d9;
        transform: scaleX(.5);
      }
    }
  }

  .input-code {
    position: absolute;
    left: -9999px;
    top: -9999px;
  }

</style>

Using the code components

<security-code v-model="code"></security-code>

Conclusion

How kind, 484 so easy

The idea is the beginning of the four input box, enter the complete monitor jumps to the next input box, such an approach can achieve the purpose, but it needs more code to maintain the rule, not elegance.

The current practice has been that I can think of the best solution if you have a better realization of ideas, but also hope the wing.

Preview Address

github address

npm address

Guess you like

Origin www.cnblogs.com/dssx/p/11784606.html