Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id="window">
  <div class="item demo-1 f-ac">
    <input v-for="(code, i) in codes.length" key="i"
         type="tel"
         ref="codes"
         maxlength="1"
         autocorrect="off"
         autocomplete="off"
         autocapitalize="off"
         spellcheck="false"
         v-model="codes[i]"
         @focus="onFocus(i)"
         @blur="onBlur(i)"
         @keyup="onKeyUp(i)"
         @keydown.delete="onDelete(i)"
         @keydown.left.right="onJump(i)"
         @paste="onPaste(i)">
  </div>
  <p>Codes: {{ codes.join('') }}</p>
</div>
            
          
!
            
              .item{
  width: 300px;
  margin-bottom: 32px;
}

.f-ac {
  display: flex;
  align-items: center;
}

.demo-1{
  input[type='tel']{
    display: block;
    width: 14%;
    margin-right: 3.2%;
    outline: none;
    padding: 10px 0;
    font-size: 20px;
    font-weight: bold;
    border: 2px solid #c7c9d3;
    border-radius: 6px;
    text-align: center;
    transition: all ease .6s;
    &:last-child{
      margin-right: 0;
    }
    &:focus{
      border-color: #333;
    }
  }
}
            
          
!
            
              new Vue({
  el: '#window',
  data:{
    codes: new Array(6).fill('')
  },
  methods:{
    onFocus (index) {
      const el = this.$refs.codes[index]
      if (el.value) {
        el.placeholder = el.value
        el.prevValue = el.value
        this.$set(this.codes, index, '')
      }
    },
    onBlur (index) {
      const el = this.$refs.codes[index]
      if (el.prevValue  && el.value === '') {
        this.$set(this.codes, index, el.prevValue)
        el.placeholder = ''
        el.prevValue = null
      }
    },
    onDelete (index) {
      const el = this.$refs.codes[index]
      // 删除内容处理
      el.prevValue = ''
      el.placeholder = ''
      el.value = ''
      if (index > 0) {
        this.$refs.codes[index - 1].focus()
      }
    },
    // 跳转到当前 Index 的下一格
    onJump (index) {
      const codesLen = this.codes.length
      let nextIndex = null
      
      if ( event.key === 'ArrowLeft' || event.key === 'Left') {
        nextIndex = index > 0 ? index - 1 : codesLen - 1
      } else if ( event.key === 'ArrowRight' || event.key === 'Right' ){
        nextIndex = index < codesLen - 1 ? index + 1 : 0
      }
      this.$refs.codes[nextIndex].focus()
    },
    onKeyUp (index) {
      const el = this.$refs.codes[index]
      let val = el.value

      // 快速输入问题解决, 将接收到输入事件,但此时 value 没有值。
      const char = event.key
      if (char.length === 1) val = char
      
      // filter
      val = val.replace(/[^0-9]/ig, "");
      
      if (!val) {
        el.focus()
      } else if (index < this.codes.length - 1){
        this.$refs.codes[index + 1].focus()
      } else {
        // 在输入到最后一个内容时,建议让其失去焦点,以免过多的输入导致最后一位内容被覆盖
        el.blur()
      }
      
      // 更新 Vue 内容
      if (val !== this.codes[index]) this.$set(this.codes, index, val)
    },
    onPaste (index) {
      const el = this.$refs.codes[index]
   
      const paste = (event.clipboardData || window.clipboardData).getData('text')
      // 过滤出符合条件的数字
      // const match = paste.match(/(?<!\d)\d{6}(?!\d)/i) //  不被兼容
      const match = ` ${paste}`.match(/[^\d](?<code>\d{6})(?!\d)/i)
      const codes = match ? match.groups.code.toString() : ''
      if(codes.length === this.codes.length && +codes == codes){
        el.blur()
        this.codes = [...codes]
      }
    }
  }
})
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console