使用CSS3 + Vue3 + js-tool-big-box工具,实现炫酷五一倒计时动效

首先,我们需要创建一个Vue组件来显示倒计时,并利用CSS3来添加动画效果。我们还会使用js-tool-big-box库来辅助我们进行日期计算。首先,确保你已经安装了Vue CLI,并创建了一个Vue项目。然后,安装js-tool-big-box库:

npm install js-tool-big-box --save

接下来,我们创建一个Vue组件,命名为Countdown.vue:

<template>
  <div class="countdown">
    <div class="number" v-for="(digit, index) in countdownDigits" :key="index">
      {{ digit }}
    </div>
  </div>
</template>

<script>
import { ref, watch, onMounted } from 'vue';
import { getRemainingTime } from 'js-tool-big-box';

export default {
  name: 'Countdown',
  setup() {
    const countdownDigits = ref([0, 0, 0, 0, 0, 0]);

    const updateCountdown = () => {
      const now = new Date();
      const targetDate = new Date(now.getFullYear(), 4, 1); // 五一劳动节日期为5月1日
      const remainingTime = getRemainingTime(now, targetDate);

      const days = Math.floor(remainingTime / (1000 * 60 * 60 * 24));
      const hours = Math.floor((remainingTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      const minutes = Math.floor((remainingTime % (1000 * 60 * 60)) / (1000 * 60));
      const seconds = Math.floor((remainingTime % (1000 * 60)) / 1000);

      countdownDigits.value = [
        Math.floor(days / 10),
        days % 10,
        Math.floor(hours / 10),
        hours % 10,
        Math.floor(minutes / 10),
        minutes % 10,
        Math.floor(seconds / 10),
        seconds % 10
      ];
    };

    onMounted(() => {
      updateCountdown();
      setInterval(updateCountdown, 1000);
    });

    return {
      countdownDigits
    };
  }
};
</script>

<style scoped>
.countdown {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 2rem;
}

.number {
  margin: 0 0.5rem;
  width: 3rem;
  height: 3rem;
  background-color: #333;
  color: #fff;
  text-align: center;
  line-height: 3rem;
  border-radius: 0.5rem;
  animation: pulse 1s infinite alternate;
}

@keyframes pulse {
  from {
    transform: scale(1);
  }
  to {
    transform: scale(1.1);
  }
}
</style>

在这个组件中,我们使用了js-tool-big-box库中的getRemainingTime函数来计算距离五一劳动节的剩余时间,并将其显示为日时分秒的形式。倒计时数字的更新使用Vue的refwatch来监听,并利用CSS3的动画效果使数字有轻微的放大和缩小动画。

最后,在你的App.vue或其他需要的地方使用这个组件:

<template>
  <div id="app">
    <Countdown />
  </div>
</template>

<script>
import Countdown from './components/Countdown.vue';

export default {
  name: 'App',
  components: {
    Countdown
  }
};
</script>

现在,你就可以在应用中看到一个炫酷的五一倒计时动效了!希望你喜欢!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叨叨爱码字

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值