您好,登錄后才能下訂單哦!
本篇內容主要講解“vue怎么實現驗證碼倒計時按鈕”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“vue怎么實現驗證碼倒計時按鈕”吧!
本文實例為大家分享了vue實現驗證碼倒計時按鈕的具體代碼,供大家參考,具體內容如下
1、點擊“發送驗證碼”按鈕后進行邏輯判斷:
?? 如果郵箱已輸入且格式正確:按鈕變為“已發送” ,此時為不可點擊狀態 并開始120s倒計時;
?? 如果郵箱未輸入或格式不正確:顯示錯誤的提示信息。
2、120s倒計時結束后按鈕變為“重新發送驗證碼” 。
html:
<div v-bind:class="{ 'text_email': isActive, 'text_tip': isTip }">{{tip}}</div> //出錯提示 <div class="input"> <i class="ret_icon-email"></i> <input type="text" v-model="email" v-bind:class="{ 'input_email0' : hasError }" v-on:click="cancelError" :placeholder="輸入郵箱地址" id="inputEmail" /> <br /> <input type="text" placeholder="輸入驗證碼" class="input_number" /> <button class="btn_number" v-bind:class="{gray:wait_timer>0}" @click="getCode()"> <span class="num_green" v-show="showNum" v-bind:class="{num:wait_timer>0}" >{{this.wait_timer + "s "}}</span> <span class="span_number" v-bind:class="{gray_span:wait_timer>0}" >{{ getCodeText() }}</span> </button> <br /> </div>
js:
data() { return { tip: "用Email找回密碼", isTip: false, isActive: true, showNum: false, wait_timer: false, hasError: false, email: "" } }, methods: { cancelError: function(event) { this.hasError = false; this.isActive = true; this.isTip = false; this.tip = "用Email找回密碼"; }, getCode: function() { if (this.wait_timer > 0) { return false; } if (!this.email) { this.hasError = true; this.isActive = false; this.isTip = true; this.tip = "Email不能為空"; return false; } if ( !/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test( this.email ) ) { this.hasError = true; this.isActive = false; this.isTip = true; this.tip = "Email地址無效"; return false; } this.showNum = true; this.wait_timer = 120; var that = this; var timer_interval = setInterval(function() { if (that.wait_timer > 0) { that.wait_timer--; } else { clearInterval(timer_interval); } }, 1000); //在這里調取你獲取驗證碼的ajax }, getCodeText: function() { if (this.wait_timer > 0) { return "已發送"; } if (this.wait_timer === 0) { this.showNum = false; return "重新發送驗證碼!"; } if (this.wait_timer === false) { return "發送驗證碼!"; } }, }
css:
.ret_icon-email { background: url(../../assets/pics/email.svg) no-repeat; //圖片為本地圖片 width: 20px; height: 20px; position: absolute; top: 12px; left: 16px; } .input_email0 { border: 1px solid rgba(239, 83, 80, 1); } .input_number { width: 112px; height: 44px; text-indent: 16px; margin-right: 12px; } .btn_number { width: 154px; height: 44px; border-radius: 4px; box-sizing: border-box; border: 1px solid rgba(76, 175, 80, 1); line-height: 16px; outline: none; } .span_number { color: rgba(76, 175, 80, 1); } .btn_number.gray { background: rgba(242, 244, 245, 1); border: 1px solid rgba(0, 0, 0, 0.05); } .span_number.gray_span { color: #9a9c9a; } .num_green.num { color: rgba(76, 175, 80, 1); }
效果圖:
到此,相信大家對“vue怎么實現驗證碼倒計時按鈕”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。