vue项目中秒杀倒计时

效果

<!-- home-seckill 秒杀 -->
<template>
  <div class="home-seckill">
    <div class="seckill-time">
      <p>距离2020年还剩:</p>
      <div class="time"><span>{{hour}}</span>: <span>{{time}}</span>: <span>{{sec}}</span></div>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件(比如:组件,工具js,第三方插件js,json文件,图片文件等等)
//例如:import 《组件名称》 from '《组件路径》';

export default {
  components: {},
  data() {
    return {
      hour: "0",
      time: "0",
      sec: "0",
      timer: null,
    };
  },
  methods: {
    seckill() {
      //获取当前事件
      let now = new Date();
      //设置目标事件
      let newtime = new Date("2022-01-01 00:00:00");
      //时间差
      let timecha = (newtime - now) / 1000;
      //计算小时
      this.hour = Math.floor(timecha / 3600);
      //计算分钟
      this.time = Math.floor((timecha % 3600) / 60);
      //计算秒
      this.sec = Math.floor(timecha % 60);
    },
  },
  computed: {},
  mounted() {
    this.timer = setInterval(this.seckill, 2000);
  },
  destroyed() {
    clearInterval(this.timer);
  },
};
</script>
<style lang='less'>
.home-seckill {
  height: 20rem;
  .seckill-time {
    height: 6rem;
    line-height: 6rem;
    display: flex;
    justify-content: center;
    .time {
      span {
        display: inline-block;
        margin-right: 0.3rem;
        padding: 0.3rem 1rem;
        border-radius: 5rem;
        background-color: black;
        font: normal 700 1.6rem "楷体";
        color: white;
      }
    }
  }
}
</style>

 有人看的话,细讲~

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值