欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS如何實現手機端輸入驗證碼效果

 更新時間:2020年05月13日 08:36:42   作者:指間流逝的夏末  
這篇文章主要介紹了JS如何實現手機端輸入驗證碼效果,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

之前在“掘金”上看到這樣一個demo 我覺得很有意思,于是今天把它搬下來,記在自己的“小本本”里
也許會對以后的項目有點用,若要自己去實現這樣一個案例也能實現,但是可能沒有那么“妙”。

想法:

1、使用label標簽做顯示驗證碼的框,

2、然后每個label for屬性指向同一個 id 為vcode 的input,

3、為了能夠觸發(fā)input焦點, 將input 改透明度樣式隱藏,

4、這樣就實現了 點擊label觸發(fā) input焦點,調用鍵盤。

運行效果:

示例代碼:

結構部分html:

<div id="app" class="app">
  <h2 class="heading-2">驗證碼:</h2>
  <div class="v-code">
    <input
        ref="vcode"
        id="vcode"
        type="tel"
        maxlength="6"
        v-model="code"
        @focus="focused = true"
        @blur="focused = false"
        :disabled="telDisabled">

    <label
        for="vcode"
        class="line"
        v-for="item,index in codeLength"
        :class="{'animated': focused && cursorIndex === index}"
        v-text="codeArr[index]"
    >
    </label>
  </div>
</div>

css部分:

<style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    body {
      background-color: #ffffff;
      font-family: -apple-system, PingFang SC, Hiragino Sans GB, Helvetica Neue, Arial;
      -webkit-tap-highlight-color: transparent;
    }
    .app {
      padding-left: 20px;
      padding-right: 20px;
      padding-top: 60px;
      max-width: 320px;
      margin-left: auto;
      margin-right: auto;
    }
    .heading-2 {
      color: #333333;
    }
    .v-code {
      margin-top: 20px;
      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: 280px;
      margin-left: auto;
      margin-right: auto;
    }
    .v-code input {
      position: absolute;
      top: 200%;
      opacity:0;
    }
    .v-code .line {
      position: relative;
      width: 40px;
      height: 32px;
      line-height: 32px;
      text-align: center;
      font-size: 28px;
    }
    .v-code .line::after {
      display: block;
      position: absolute;
      content: '';
      left: 0;
      width: 100%;
      bottom: 0;
      height: 1px;
      background-color: #aaaaaa;
      transform: scaleY(.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>

Javascript部分

1、通過CDN引入vue.js

<script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script>

2、代碼

var app = new Vue({
    el: '#app',
    data: {
      code: '',
      codeLength: 6,
      telDisabled: false,
      focused: false
    },
    computed: {
      codeArr() {
        return this.code.split('')
      },
      cursorIndex() {
        return this.code.length
      }
    },
    watch: {
      code(newVal) {
        this.code = newVal.replace(/[^\d]/g,'')
        if (newVal.length > 5) {
          // this.telDisabled = true
          this.$refs.vcode.blur()
          setTimeout(() => {
            alert(`vcode: ${this.code}`)
          }, 500)
        }
      }
    },
  })

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • firefox下frameset取不到值的解決方法

    firefox下frameset取不到值的解決方法

    IE FF都可以直接通過Frame的Name來訪問,但是FrameSet不支持name屬性
    2010-09-09
  • uniapp退出關閉當前小程序或APP的簡單實現

    uniapp退出關閉當前小程序或APP的簡單實現

    最近通過Uniapp開發(fā)APP又一個非常實用的功能,這篇文章主要給大家介紹了關于uniapp退出關閉當前小程序或APP的簡單實現,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-12-12
  • 詳解JavaScript中浮點數的精度計算

    詳解JavaScript中浮點數的精度計算

    這篇文章主要來和大家介紹一下JavaScript中浮點數精度計算的相關知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-06-06
  • js+css實現select的美化效果

    js+css實現select的美化效果

    這篇文章主要為大家詳細介紹了js+css實現select的美化效果,如何針對select進行美化,感興趣的小伙伴們可以參考一下
    2016-03-03
  • js實現復選框的全選和取消全選效果

    js實現復選框的全選和取消全選效果

    在很多網站都有這樣的功能,當點擊一個全選按鈕之后,所有的復選框都會被選中,再點擊之后會取消全選,功能非常的人性化,可以省卻很多人力,本文將簡單介紹一下JS如何實現此功能
    2017-01-01
  • 在Iframe中獲取父窗口中表單的值(示例代碼)

    在Iframe中獲取父窗口中表單的值(示例代碼)

    這篇文章主要介紹了在Iframe中獲取父窗口中表單的值(示例代碼)。需要的朋友可以過來參考下,希望對大家有所幫助
    2013-11-11
  • 鼠標選擇動態(tài)改變網頁背景顏色的JS代碼

    鼠標選擇動態(tài)改變網頁背景顏色的JS代碼

    這篇文章主要介紹了鼠標選擇動態(tài)改變網頁背景顏色的JS代碼,有需要的朋友可以參考一下
    2013-12-12
  • 原生JS實現拖拽功能

    原生JS實現拖拽功能

    這篇文章主要為大家介紹了JS實現拖拽功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • 深入理解JavaScript事件機制

    深入理解JavaScript事件機制

    事件機制是幾乎所有開發(fā)語言都有的機制,并不是deviceone的獨創(chuàng),在某些語言稱之為消息(Event),有些地方稱之為(Message).接下來通過本文給大家介紹JS事件機制的理解 ,需要的朋友一起學習吧
    2023-04-04
  • javascript xml為數據源的下拉框控件

    javascript xml為數據源的下拉框控件

    此控件以xml為數據源,可以進行輸入的多屬性自動適配
    2009-07-07

最新評論