vue2.0倒計時demo
阿新 • • 發佈:2018-12-04
vue2.0倒計時demo
<template > <div class="register-mengceng"> <div class="register-head"> <span><</span> <h3>免費註冊</h3> </div> <form action=""> <div class="register-tel"> <input type="text" placeholder="請輸入您的手機號 " v-model="register_tel"/> </div> <div class="register-code"> <input type="text" placeholder="請輸入驗證碼 " v-model="register_code"/> <button :disabled="disabled" type="button" id="myButton" data-loading-text="Loading..." class="btn btn-primary" v-on:click="metnod_code" :class="{disabled: !this.canClick}"> {{bntVal}} </button> </div> <div class="regster-agree"> <input type="checkbox" /> <span>我已閱讀《*****》相關事宜</span> </div> <button type="submit" id="subbnt" data-loading-text="Loading..." class=" btn1 btn btn-primary" autocomplete="off"> 註冊 </button> </form> <div class="register-chat"> <span class="register-line"></span><span class="register-font">第三方註冊登入</span><span class="register-line"></span> <img src="../../assets/img/logo.png" alt="" /> </div> </div> </template> <script> export default { name: 'Register', data() { return { register_tel:"", register_code:"", bntVal:"獲取驗證碼",//按鈕內容 totalTime:60,//倒計時時間 canClick:true,//預設可點選樣式 disabled:false,//預設可點選 } }, methods:{ metnod_code: function(){ this.canClick=false;//不可點選的樣式 this.disabled=true;//新增不可點選的屬性 this.bntVal = this.totalTime + 's後重發' //這裡解決60秒不見了的問題 let clock = window.setInterval(() => {//使用=>可以直接使用this this.totalTime--;//setInterval超時呼叫; this.bntVal = this.totalTime + 's後重發' if (this.totalTime < 0) { //當倒計時小於0時清除定時器 this.canClick=true;//恢復點選樣式 this.disabled=false;//恢復點選 window.clearInterval(clock) this.bntVal = '獲取驗證碼' this.totalTime = 60 } },1000) } } } </script> <style> .register-head{ height: 80px; width: 100%; border-bottom: 5px solid #E6E6E6; margin-bottom: 5%; } .register-head h3{ display: inline-block; text-align: center; line-height: 80px; margin-top: auto; letter-spacing: 8px; } .register-head span{ display: inline-block; float: left; line-height: 80px; font-size: 32px; margin-left: 28px; color: forestgreen; } form{ width: 80%; margin: auto; position: relative; } form .register-code input,form .register-tel input{ width: 100%; line-height: 2.5; border: none; border-bottom: 1px solid #E6E6E6; outline: none; } form .register-tel{ margin-bottom: 30px; } form .register-code { position: relative; } form #myButton{ position: absolute; width: 25%; right: 0; outline: none; top: -10px; } form .regster-agree{ position: absolute; left: 0; margin: 15px 0; } form #subbnt{ width: 60%; margin: auto; margin-top: 60px; } .register-chat{ margin-top: 60px; } .register-chat .register-font{ display: inline-block; margin: 0 15px; } .register-chat .register-line{ width: 30%; border: 1px solid #E6E6E6; display: inline-block; } .register-chat img{ display: block; margin: 15px auto ; } .btn1 { background-color: #E6E6E6; border: none; color: black; } .disabled{ background-color: #ddd; border-color: #ddd; color:#57a3f3; cursor: not-allowed; disabled:disabled; } /*媒體查詢,參考部分Bootstrap 框架*/ /*在768 和991 畫素之間的螢幕裡,小螢幕,主要是PAD*/ @media (min-width: 768px) and (max-width: 991px) { } /*在480 和767 畫素之間的螢幕裡,超小螢幕,主要是手機*/ @media (min-width: 480px) and (max-width: 767px) { .register-head h3{font-size: 22px;} } /*在小於480 畫素的螢幕,微小螢幕,更低解析度的手機*/ @media (max-width: 479px) { .register-head{height: 60px;} .register-head h3{line-height: 60px;} .register-head span{line-height: 60px;} .register-head h3{font-size: 16px;} form .register-tel,.register-code{font-size: 12px;} form .btn{font-size: 9px;} form #myButton{width: 31%;} } </style>