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>