vue封装倒计时:

文章介绍了如何在Vue中创建两个倒计时组件,一个接收结束日期字符串,另一个接收时间间隔数组。组件实现了动态计算并显示剩余天数、小时、分钟和秒数,同时包含了时间格式化和页面刷新后的重新计时功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

readme:

```javascript
倒计时组件:

1、index.vue   输入倒计时结束的时间:"2023-6-24 16:00:00"
    引用:
       import CountDown from '@/components/CountDown/index.vue'
    使用:
       <CountDown time="2023-6-24 16:00:00"></CountDown>
      注意:1、输入为字符串:   所以    time="2023-6-24 16:00:00"
            



2、index2.vue   输入倒计时所经历的时间:[3,0,0,0]    (【天,时,分,秒】)(3天)
    引用:
       import CountDown2 from '@/components/CountDown/index2.vue'
    使用:
       <CountDown2 :time=[3,0,0,0]></CountDown2>
        注意: 1、输入为数组:   所以    :time=[3,0,0,0]
               2、 刷新页面会重新计时

index1.vue

<script setup>
import { onMounted,onUnmounted,ref } from 'vue';

const props=defineProps({
    time:{
        type:String
    }
})

//时间到了:
const timeover=ref(false)
const timeList=ref([0,0,0,0])
const 单位=['天','时','分','秒']
let timer=null
//倒计时函数:每一秒执行一次


const CountDown=()=>{
    let nowTime=+new Date()
    let inputTime=+new Date(props.time)
    let times = (inputTime - nowTime) / 1000
    if( times>=1){
      // 获取剩余天数
      timeList.value[0] = format(parseInt(times / 60 / 60 / 24))
      // 获取剩余小时
      timeList.value[1]= format(parseInt(times / 60 / 60 % 24))
      // 获取剩余分钟
      timeList.value[2] = format(parseInt(times / 60 % 60))
      // 获取剩余秒
      timeList.value[3] = format(parseInt(times % 60))
      // 返回倒计时封装信息的字符串
    }else{
        timeList.value[0]='00'
        timeList.value[1]='00'
        timeList.value[2]='00'
        timeList.value[3]='00'
        timeover.value=true
    }
      
      
}
//修正  把 8秒 变成08 秒
const format =(x)=>{
    return x>=10?x:`0${x}`
}

onMounted(()=>{
  //先执行一次 防止刚打开页面要等1秒才出现倒计时
   CountDown()
   timer=setInterval(CountDown,1000)
})
//切换组件关闭定时器
onUnmounted(()=>{
   clearInterval(timer)
})
</script>


<template>

 <div class="box" :class="{'timeover':timeover}">
    <div class="minibox" v-for="(item,index) in timeList" :key="index"> 
       <div class="time">{{ item }}</div> 
       <div class="danwei">{{ 单位[index] }}</div> 
    </div>
 </div>

</template>


<style scoped lang="scss">
.box{
    display: flex;
    height: 100px;
    width: 400px;
    border: 2px solid silver;
    justify-content:space-between;
    align-items: center;
   
    .minibox{
        position: relative;
        .time{
            height: 80px;
            width: 80px;
            background-color: black;
            color: aliceblue;
            font-size: 40px;
            line-height: 80px;
            margin: 5px;
            text-align: center;
            border-radius: 10px;
        }
        .danwei{
            position: absolute;
            height: 20px;
            width: 20px;
            color: aliceblue;
            font-size: 20px;
            right: 5px;
            bottom: 5px;
       }
    }
    
}
.timeover{
    background-color: red;
}
</style>

index2.vue

<script setup>
import { computed, onMounted,onUnmounted,ref } from 'vue';

const props=defineProps({
    time:{
        type:Array
    }
})
//计算一共多少秒
const timeToSecond=computed(()=>{
    return props.time[0]*24*60*60+props.time[1]*60*60+props.time[2]*60+props.time[3]
})

const 单位=['天','时','分','秒']
let timer=null
//倒计时函数:每一秒执行一次
const xxx=ref(timeToSecond.value)
// 更改格式:
const timeList=computed(()=>{
    let d=0
    let h=0
    let m =0
    let s=0
    let timrover=false
    if(xxx.value>0) {
        d = format(parseInt(xxx.value / 60 / 60 / 24))
      // 获取剩余小时
       h= format(parseInt(xxx.value / 60 / 60 % 24))
      // 获取剩余分钟
      m = format(parseInt(xxx.value / 60 % 60))
      // 获取剩余秒
       s = format(parseInt(xxx.value % 60))
    }else{
        timrover=true
         d='00'
         h='00'
         m ='00'
         s='00'
    }
     
    return {time:[d,h,m,s],timeover:timrover}
})
//修正  把 8秒 变成08 秒
const format =(x)=>{
    return x>=10?x:`0${x}`
}

onMounted(()=>{
  //先执行一次 防止刚打开页面要等1秒才出现倒计时
   
   timer=setInterval(()=>{
    xxx.value--
   
   },1000)
})
//切换组件关闭定时器
onUnmounted(()=>{
   clearInterval(timer)
})
</script>


<template>

 <div class="box" :class="{'timeover':timeList.timeover}">
     <div class="minibox" v-for="(item,index) in timeList.time" :key="index"> 
       <div class="time">{{ item }}</div> 
       <div class="danwei">{{ 单位[index] }}</div> 
    </div> 
 </div>

</template>


<style scoped lang="scss">
.box{
    display: flex;
    height: 100px;
    width: 400px;
    border: 2px solid silver;
    justify-content:space-between;
    align-items: center;
   
    .minibox{
        position: relative;
        .time{
            height: 80px;
            width: 80px;
            background-color: black;
            color: aliceblue;
            font-size: 40px;
            line-height: 80px;
            margin: 5px;
            text-align: center;
            border-radius: 10px;
        }
        .danwei{
            position: absolute;
            height: 20px;
            width: 20px;
            color: aliceblue;
            font-size: 20px;
            right: 5px;
            bottom: 5px;
       }
    }
    
}
.timeover{
    background-color: red;
}
</style>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值