倒计时demo

<!-- 倒计时 -->
<template>
  <view class="flex a-c j-c event-down" :style="{minWidth:`${minWidth}`,textAlign:`${textAlign}`}">
	<view class="down-item br-8 flex a-c j-c bold col-fff f-24 lin-24" v-if="Number(day) > 0">
		{{day}}
	</view>
	<view v-if="Number(day) > 0" class="col-fff f-24 lin-24 bold flex a-c j-c down-icon">:</view>
	<view class="down-item br-8 flex a-c j-c bold col-fff f-24 lin-24">
		{{hou}}
	</view>
	<view class="col-fff f-24 lin-24 bold flex a-c j-c down-icon">:</view>
	<view class="down-item br-8 flex a-c j-c bold col-fff f-24 lin-24">
		{{min}}
	</view>
	<view class="col-fff f-24 lin-24 bold flex a-c j-c down-icon">:</view>
	<view class="down-item br-8 flex a-c j-c bold col-fff f-24 lin-24">
		{{sec}}
	</view>
  </view>
</template>

<script>
export default {
  name: "count-down",
  props: {
	//开始时间
    currentTime: {
      type: [String, Number],
      default: 0
    },
	//结束时间
    endTime: {
      type: [String, Number],
      default: 0
    },
	minWidth:{
	  type:String,
	  default:'100rpx'
	},
	textAlign:{
	  type:String,
	  default:'left'
	}
  },
  data() {
    return {
      day: '00',
      hou: '00',
      min: '00',
      sec: '00',
    }
  },
  created() {
    this.countDown(this.currentTime, this.endTime)
  },
  beforeDestroy(){
	clearInterval(this.interval);
  },
  methods: {
    timeFormat(param) {
      return param < 10 ? '0' + param : param;
    },
    countDown(newTime, endTime) {
      clearInterval(this.interval);
      this.interval = null;
      this.interval = setInterval(() => {
        // 对结束时间进行处理渲染到页面
        let obj = null;
        // 如果活动未结束,对时间进行处理
        newTime = newTime + 1000;
        if (endTime - newTime > 0) {
          let time = (endTime - newTime) / 1000;
          // 获取天、时、分、秒
          let day = parseInt(time / (60 * 60 * 24));
          let hou = parseInt(time % (60 * 60 * 24) / 3600);
          let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
          let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
          obj = {
            day: this.timeFormat(day),
            hou: this.timeFormat(hou),
            min: this.timeFormat(min),
            sec: this.timeFormat(sec)
          };
        } else { // 活动已结束,全部设置为'00'
          obj = {
            day: '00',
            hou: '00',
            min: '00',
            sec: '00'
          };
          clearInterval(this.interval);
        }
        this.day = obj.day;
        this.hou = obj.hou;
        this.min = obj.min;
        this.sec = obj.sec;
      }, 1000);
    },
  }
}
</script>

<style scoped lang="scss">
.date {
  white-space: nowrap;
}
.event-down{
	.down-item{
		width: 40rpx;
		height: 40rpx;
		background: #141414;
		box-sizing: border-box;
	}
	.down-icon{
		width: 28rpx;
		height: 40rpx;
	}
}
</style>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值