uni-app 倒计时组件

<template>
	<view class="time" :style="justifyLeft">
		<text class="red" v-if="tipText">{{ tipText }}</text>
		<text class="styleAll" v-if="isDay === true">{{ day }}</text>
		<text class="timeTxt red" v-if="dayText">{{ dayText }}</text>
		<text class="styleAll">{{ hour }}</text>
		<text class="timeTxt red" v-if="hourText">{{ hourText }}</text>
		<text class="styleAll">{{ minute }}</text>
		<text class="timeTxt red" v-if="minuteText">{{ minuteText }}</text>
		<text class="styleAll">{{ second }}</text>
		<text class="timeTxt red" v-if="secondText">{{ secondText }}</text>
	</view>
</template>

<script>
	export default {
		name: "countDown",
		props: {
			justifyLeft: {
				type: String,
				default: ""
			},
			//距离开始提示文字
			tipText: {
				type: String,
				default: "倒计时"
			},
			dayText: {
				type: String,
				default: "天"
			},
			hourText: {
				type: String,
				default: "时"
			},
			minuteText: {
				type: String,
				default: "分"
			},
			secondText: {
				type: String,
				default: "秒"
			},
			datatime: {
				type: Number,
				default: 0
			},
			isDay: {
				type: Boolean,
				default: true
			},
			id:{
				type: String | Number,
				default: ""
			}
		},
		data: function() {
			return {
				day: "00",
				hour: "00",
				minute: "00",
				second: "00"
			};
		},
		created: function() {
			this.show_time();
		},
		mounted: function() {},
		methods: {
			show_time: function() {
				let that = this;

				function runTime() {
					//时间函数
					let intDiff = that.datatime - Date.parse(new Date()) / 1000; //获取数据中的时间戳的时间差;
					let day = 0,
						hour = 0,
						minute = 0,
						second = 0;
					if (intDiff > 0) {
						//转换时间
						if (that.isDay === true) {
							day = Math.floor(intDiff / (60 * 60 * 24));
						} else {
							day = 0;
						}
						hour = Math.floor(intDiff / (60 * 60)) - day * 24;
						minute = Math.floor(intDiff / 60) - day * 24 * 60 - hour * 60;
						second =
							Math.floor(intDiff) -
							day * 24 * 60 * 60 -
							hour * 60 * 60 -
							minute * 60;
						if (hour <= 9) hour = "0" + hour;
						if (minute <= 9) minute = "0" + minute;
						if (second <= 9) second = "0" + second;
						that.day = day;
						that.hour = hour;
						that.minute = minute;
						that.second = second;
					} else {
						that.day = "00";
						that.hour = "00";
						that.minute = "00";
						that.second = "00";
						clearInterval(runTime)
						uni.$emit('endTime',that.id)
					}
				}
				runTime();
				setInterval(runTime, 1000);
			}
		}
	};
</script>

<style>
	.time{
		display: flex;
		justify-content: center;
	} 
	.red{
		color: #fc4141;
		margin: 0 4rpx;
	}
</style>

剩余

<CountDown :is-day="false" :tip-text="' '" :day-text="' '" :hour-text="' : '" :minute-text="' : '" :second-text="' '"

:datatime="combinationData.end_time"></CountDown>

结束

相关推荐
一路向前的月光1 分钟前
React(7)
前端·javascript·react.js
helloweilei11 分钟前
Webpack中使用@svgr/webpack处理SVG文件
前端
2301_7930698215 分钟前
前后端分离的网页游戏,后端spring boot,前端vite+vue
前端·后端·vue·springboot·全栈
刺客-Andy25 分钟前
React 第二十五节 <Fragment></Fragment> 的用途以及使用注意事项详解
前端·react.js·前端框架
今朝有熊30 分钟前
10-8 根据选中类别动态生成首页模块的顶部标签导航器和modal
前端
GitCode官方38 分钟前
Flutter 双屏双引擎通信插件加入 GitCode:解锁双屏开发新潜能
前端·flutter·gitcode
Creator_Ly1 小时前
4-电脑一连接上自动弹框到路由器web配置页面
前端·智能路由器·电脑·openwrt
songbaoxian1 小时前
【前端】ES6新特性汇总
前端·ecmascript·es6
是小圆子呀1 小时前
vue相关的面试题
前端·javascript·vue.js·面试
Palpitate_LL1 小时前
Apache iotdb-web-workbench 认证绕过漏洞 CVE-2023-24829
前端·apache·iotdb