编写一段vue3的五一假期倒计时
时间: 2023-10-17 16:03:15 浏览: 62
基于vue的过年倒计时的代码
好的,以下是一段简单的Vue3组件代码,用于实现五一假期倒计时:
```html
<template>
<div>
<h2>距离五一假期还有:</h2>
<div class="countdown">
<div>{{ days }}</div>
<span>天</span>
<div>{{ hours }}</div>
<span>小时</span>
<div>{{ minutes }}</div>
<span>分钟</span>
<div>{{ seconds }}</div>
<span>秒</span>
</div>
</div>
</template>
<script>
import { ref } from 'vue';
export default {
setup() {
const countdownDate = new Date('2022-05-01');
const days = ref(0);
const hours = ref(0);
const minutes = ref(0);
const seconds = ref(0);
const updateCountdown = () => {
const now = new Date().getTime();
const distance = countdownDate - now;
days.value = Math.floor(distance / (1000 * 60 * 60 * 24));
hours.value = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
minutes.value = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
seconds.value = Math.floor((distance % (1000 * 60)) / 1000);
};
setInterval(updateCountdown, 1000);
return {
days,
hours,
minutes,
seconds,
};
},
};
</script>
```
该组件使用了Vue3的Composition API,通过`ref`函数创建了四个响应式变量:`days`、`hours`、`minutes`和`seconds`,并使用`setInterval`函数每秒更新一次倒计时。在模板中,这些变量被渲染为四个数字,并带有相应的文本标签。
阅读全文