码迷,mamicode.com
首页 > 其他好文 > 详细

vue组件系列-验证码倒计时组件

时间:2020-01-17 20:43:41      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:jpg   输入   bsp   height   activity   url   size   class   des   

<style scoped>
    .veri-code{
        width: 100%;
        .veri-send{
            width: 100%;
            height: 91px;
            position:relative;
            overflow:hidden;
            >.sendBtn{
                background:url(../assets/images/hongbao_code_btn.jpg) 100% 100% no-repeat;
                background-size: contain;
                display: inline-block;
                width: 171px;
                height: 91px;
                z-index:2;
                position: absolute;
                right:0;
                top:0;
                span{
                    display: inline-block;
                    width: 171px;
                    height: 91px;
                    line-height: 91px;
                    text-align: center;
                    font-family: PingFangSC;
                    font-size: 32px;
                    font-weight: 600;
                    color: #c31222;
                }
            }
        }
        .veri-activated{
            >.sendBtn{
                span{
                    background:url(../assets/images/hongbao_code_btn_disabled.jpg) 100% 100% no-repeat;
                    background-size: contain;
                    color:#8e0612;
                }
            }
        }
    }
</style>

<template>
    <div class="veri-code">
        <div :class="[‘veri-send‘,{‘veri-activated‘:isSended || activityEnd}]">
            <txt-input placeholder="请输入手机号" max-length="11" type="tel"></txt-input>
            <div class="sendBtn">
                <span v-if="!isSended" @click="getVeriCode">验证码</span>
                <span v-if="isSended">{{seconds}}s</span>
            </div>
        </div>
    </div>
</template>

<script>
    import Vue from ‘vue‘;
    import TxtInput from ‘./TxtInput.vue‘;
    import bus from ‘../lib/bus‘;
    export default Vue.extend({
        name:‘veri-code‘,
        components:{
            ‘txt-input‘:TxtInput
        },
        data(){
            return {
                seconds:60,
                left_time:0,
                isSended: false
            }
        },
        props: {
            sended: {
                default: this.isSended
            },
            activityEnd:{
                default: false
            }
        },
        watch:{
            sended: function(val,oldVal){
                this.isSended = this.sended;
                if(this.sended & this.left_time == 0){
                    let _this = this,flag = this.seconds;
                    let setSended = () => {
                        clearInterval(timmer);
                        _this.isSended = false;
                        _this.seconds = flag;
                        _this.$parent.codeSended = false;
                    };
                    let timmer = setInterval(function(){
                        _this.seconds > 1 ? _this.seconds-- : setSended()
                    },1000);
                }
            },
            left_time: function(val,oldVal){
                this.defaultState()
            }
        },
        methods: {
            getVeriCode: function(){
                this.$parent.getVeriCode()
            },
            defaultState: function(){
              if(this.left_time != 0){
                let _this = this,flag = this.seconds;
                this.seconds = this.left_time
                this.sended = true
                let setSended = () => {
                    clearInterval(timmer);
                    _this.sended = false;
                    _this.seconds = flag;
                    this.left_time = 0;
                };
                let timmer = setInterval(function(){
                    _this.seconds > 0 ? _this.seconds-- : setSended()
                },1000);
              }
            },
        },
        // created(){
        //   this.defaultState()
        // }
    })
</script>

 

vue组件系列-验证码倒计时组件

标签:jpg   输入   bsp   height   activity   url   size   class   des   

原文地址:https://www.cnblogs.com/sybboy/p/12207230.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!