js_秒杀倒计时

样图展示

在这里插入图片描述

html模块

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>距离双12倒计时</title>
		<link rel="stylesheet" href="css/ms.css">
	</head>
	<body>
		<div class="countdown">
			<h2>秒杀时间</h2>
			<p id="targetTime">2022-12-12 0:0:0</p>
			<div class="show">
				<div class="day">
					<div class="number">10</div>
					<span class="text"></span>
				</div>
				<div class="day">
					<div class="number">06</div>
					<span class="text">小时</span>
				</div>
				<div class="day">
					<div class="number">16</div>
					<span class="text"></span>
				</div>
				<div class="day">
					<div class="number">51</div>
					<span class="text"></span>
				</div>
			</div>
		</div>
		<script src="js/ms.js" type="text/javascript" charset="utf-8"></script>
	</body>
</html>

css模块

.countdown{
	display: flex;
	flex-direction: column;
	justify-content: space-around;
	border: 1px solid #000;
	/* width:50%; */
	margin: 0 auto;
	text-align: center;
	background: linear-gradient( rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5) ),url(../img/bg.jpg)no-repeat center center;
	background-size: 100% 220%;
	overflow: hidden;
	color:#fff;
	font-size: 24px;
	padding-top: 20px;
}
.show{
	display: flex;
	justify-content: space-between;
	box-sizing: border-box;
	padding: 20px 30% 50px;
}
.day{
	display: flex;
	align-items: center;
}
.day .number{
	font-size: 30px;
}
.day .text{
	font-size: 15px;
	padding-left: 10px;
}

js模块

window.onload = function(){
	//计算相差的秒数并返回
	function calcTime(){
		let future = document.getElementById('targetTime').innerHTML;
		future = new Date('2022/12/12 00:00:00');
		// 获取现在的时间
		let now = new Date()
		console.log(future)
		console.log(now)
		// 计算时间差
		let diff = Math.ceil((future.getTime() - now.getTime()) / 1000);
		return diff;
	}
	
	// let timer = null
	let timer = setInterval(function(){
		// 在页面上显示着一刻的倒计时
		function showTime(s){
			// 将秒数转化为相应的天数、小时、分、秒
			let days = Math.floor(s / 24 / 60 / 60)
			let hours = Math.floor(s / 60 / 60) % 24
			let minutes = Math.floor(s / 60) % 60
			let seconds = s % 60
			
			// 不足2位,前面补0
			days = format(days)
			hours = format(hours)
			minutes = format(minutes)
			seconds = format(seconds)
			
			// 在页面上显示
			document.querySelector('.show').innerHTML = `<div class="show">
					<div class="day">
						<div class="number">${days}</div>
						<span class="text">天</span>
					</div>
					<div class="day">
						<div class="number">${hours}</div>
						<span class="text">小时</span>
					</div>
					<div class="day">
						<div class="number">${minutes}</div>
						<span class="text">分</span>
					</div>
					<div class="day">
						<div class="number">${seconds}</div>
						<span class="text">秒</span>
					</div>
				</div>`;
			if (s == 0){
				document.querySelector('.show').innerHTML = `<h1>&nbsp;&nbsp;&nbsp;&nbsp;开始抢购!</h1>`;
				clearInterval(timer)
			}
		}
		
		function format(t){
			if (t < 10){
				t = '0' + t;
			} else {
				t = t.toString();
			}
			return t
		}
		showTime(calcTime());
	}, 1000)
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值