列表页面多个倒计时

这是一个关于Vue.js组件的示例,展示如何在列表中实现多个倒计时功能。代码创建了一个列表,每个列表项包含一个倒计时,通过`v-for`循环遍历数据,并使用`mounted`钩子函数初始化倒计时。`countdown`方法用于格式化倒计时显示,而`daojishi`方法则负责更新每个倒计时的显示值。
摘要由CSDN通过智能技术生成

列表页面多个倒计时


在这里插入图片描述

<template>
	<div class="home-wrap">
		<ul class="home-list">
			<template v-for="(item,index) in listArray">
				<li class="home-item" v-bind:countdown = "item.time" :key="index">
					<span>{{item.name}}</span>
					<span :id="'countdown'+index"></span>
				</li>
			</template>
		</ul>
	</div>
</template>

<script>
	export default {
		name:"home",
		data(){
			return {
				listArray:[{
					time:36000,
					name:"支付"
				},{
					time:754,
					name:"支付"
				},{
					time:457,
					name:"支付"
				},{
					time:3567,
					name:"支付"
				}]
			}
		},
		mounted() {
			document.getElementsByClassName("home-item").forEach((item,index)=>{
				let countdown=item.getAttribute("countdown");
				this.daojishi(countdown,"countdown"+index)
			})
		},
		computed:{
			
		},
		methods:{
			countdown(result){
				var h = Math.floor(result / 3600) < 10 ? '0'+Math.floor(result / 3600) : Math.floor(result / 3600);
				var m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
				var s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
				if(h==0){
					result = m + ":" + s;
				}else{
					result = h+':'+m + ":" + s
				}    
				return result;
			},
			daojishi(time,handle){
				var timer=setInterval(()=>{
					if(time>=0){
						document.getElementById(handle).innerHTML=this.countdown(time)
						time--
					}else{
						document.getElementById(handle).innerHTML=" "
						clearInterval(timer)
					}	 
				},1000)
			}
		},
		destroyed() {
			location.reload()
		}
	}
</script>

<style  lang="scss" scoped="scoped">
	.home-wrap{
		width: 91.47%;
		display: flex;
		flex-direction: column;
		margin: 20px auto;
		.home-list{
			border: 1px solid #f5f4f4;
			display: flex;
			flex-direction: column;
			.home-item{
				border-bottom: 1px solid #f5f4f4;
				height: 50px;
				line-height: 50px;
				text-align: center;
			}
			.home-item:last-child{
				border-bottom: 10;
			}
		}
	}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值