<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        vue實現驗證碼輸入框組件

        來源:懂視網 責編:小采 時間:2020-11-27 22:23:41
        文檔

        vue實現驗證碼輸入框組件

        vue實現驗證碼輸入框組件:先來看波完成效果圖 需求 輸入4位或6位短信驗證碼,輸入完成后收起鍵盤 實現步驟 第一步 布局排版 <div class=security-code-wrap> <label for=code> <ul class=security-code-container>
        推薦度:
        導讀vue實現驗證碼輸入框組件:先來看波完成效果圖 需求 輸入4位或6位短信驗證碼,輸入完成后收起鍵盤 實現步驟 第一步 布局排版 <div class=security-code-wrap> <label for=code> <ul class=security-code-container>

        先來看波完成效果圖

         

        需求

        輸入4位或6位短信驗證碼,輸入完成后收起鍵盤

        實現步驟

        第一步

        布局排版

        <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>

        使用li元素來模擬輸入框的顯示,沒有別的目的,就只是為了語義化,當然你也可以使用其他任意一個元素來模擬,比如div。

        使用label標簽的好處在于它可以跟input的click事件關聯上,一方面實現了語義化解決方案,另一方面也省去了我們通過js來喚起虛擬鍵盤。

        隱藏輸入框

        .input-code {
         position: absolute;
         left: -9999px;
         top: -99999px;
         width: 0;
         height: 0;
         opacity: 0;
         overflow: visible;
         z-index: -1;
        }

        將真實的輸入框定位到屏幕可視區域以外的地方,虛擬鍵盤被喚起時,就不會將頁面往上頂了。所以你的驗證碼輸入組件一定要放在虛擬鍵盤遮擋不了的地方。

        第二步

        處理驗證碼輸入

        handleSubmit() {
         this.$emit('input', this.value)
        },
        handleInput(e) {
         this.$refs.input.value = this.value
         if (this.value.length >= this.number) {
         this.hideKeyboard()
         }
         this.handleSubmit()
        }

        輸入時,給輸入框賦一次值,是為了解決android端上輸入框失焦后重新聚焦,輸入光標會定在第一位的前面,經過賦值再聚焦,光標的位置就會顯示在最后一位后面。

        第三步

        完成輸入后關閉虛擬鍵盤

        hideKeyboard() {
         // 輸入完成隱藏鍵盤
         document.activeElement.blur() // ios隱藏鍵盤
         this.$refs.input.blur() // android隱藏鍵盤
        }

        組件完整代碼

        <!--四位驗證碼輸入框組件-->
        <template>
         <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>
        </template>
        <script>
         export default {
         name: 'SecurityCode',
         // component properties
         props: {
         number: {
         type: Number,
         default: 4
         },
         placeholder: {
         type: String,
         default: '-'
         }
         },
         // variables
         data() {
         return {
         value: ''
         }
         },
         methods: {
         hideKeyboard() {
         // 輸入完成隱藏鍵盤
         document.activeElement.blur() // ios隱藏鍵盤
         this.$refs.input.blur() // android隱藏鍵盤
         },
         handleSubmit() {
         this.$emit('input', this.value)
         },
         handleInput(e) {
         this.$refs.input.value = this.value
         if (this.value.length >= this.number) {
         this.hideKeyboard()
         }
         this.handleSubmit()
         }
         }
         }
        </script>
        <style scoped lang="less">
         .security-code-wrap {
         overflow: hidden;
         }
         .security-code-container {
         margin: 0;
         padding: 0;
         display: flex;
         justify-content: center;
         .field-wrap {
         list-style: none;
         display: block;
         width: 40px;
         height: 40px;
         line-height: 40px;
         font-size: 16px;
         background-color: #fff;
         margin: 2px;
         color: #000;
         .char-field {
         font-style: normal;
         }
         }
         }
         .input-code {
         position: absolute;
         left: -9999px;
         top: -99999px;
         width: 0;
         height: 0;
         opacity: 0;
         overflow: visible;
         z-index: -1;
         }
        </style>

        組件使用代碼

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

        總結

        以上所述是小編給大家介紹的vue實現驗證碼輸入框組件,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        vue實現驗證碼輸入框組件

        vue實現驗證碼輸入框組件:先來看波完成效果圖 需求 輸入4位或6位短信驗證碼,輸入完成后收起鍵盤 實現步驟 第一步 布局排版 <div class=security-code-wrap> <label for=code> <ul class=security-code-container>
        推薦度:
        標簽: VUE 組件 輸入框
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 亚洲AV无码码潮喷在线观看| 亚洲?V乱码久久精品蜜桃| 亚洲av无码一区二区三区网站| 欧美亚洲精品一区二区| 免费A级毛片在线播放不收费| 亚洲av乱码一区二区三区香蕉| 91制片厂制作传媒免费版樱花| 日本免费中文字幕在线看| 亚洲狠狠婷婷综合久久蜜芽| 波多野结衣久久高清免费| 国产精品亚洲一区二区无码| 亚洲AV无码一区二区三区国产| 国产高潮流白浆喷水免费A片 | 亚洲AV午夜成人片| 又粗又大又黑又长的免费视频| 亚洲高清毛片一区二区| 亚洲精品老司机在线观看| 久久免费动漫品精老司机| 亚洲一级毛片在线播放| 国产乱人免费视频| 久久成人永久免费播放| 亚洲综合色丁香麻豆| 在线免费一区二区| 亚洲αv在线精品糸列| 91精品免费国产高清在线| 亚洲精品无码少妇30P| 国产亚洲精品无码专区 | 亚洲AV无码久久精品狠狠爱浪潮| 毛片在线播放免费观看| 亚洲AV色吊丝无码| 亚洲人成人网站在线观看| 精品无码人妻一区二区免费蜜桃| 亚洲人成色99999在线观看| 亚洲精品二区国产综合野狼| 国产香蕉免费精品视频| 特级av毛片免费观看| 亚洲综合一区二区精品导航| 国产成人精品123区免费视频| 爱丫爱丫影院在线观看免费| 亚洲人成色在线观看| 亚洲成人精品久久|