变色时钟和倒计时器功能描述
- 时钟的颜色会随秒数随机变换
- 输入截止时间页面显示当前时间和距离截止剩余时间
实现效果
变色时钟和倒计时器实现步骤
1. 准备阶段
- 获得对应范围的随机数
代码 | 含义 |
---|---|
Math.floor(Math.random()*(n+1)) | 获取0到n的随机数 |
Math.floor(Math.random()*arr.length) | 获取数组长度的随机数 |
Math.floor(Math.random()*(m-n+1))+1 | 获取n到m的随机数 |
- 获取时间戳:自1970年1月1日至当前时间的总秒数。为毫秒,需提前除1000
代码 | 含义 |
---|---|
const now = +new Date() | 当前时间的时间戳 |
const preTime = +new Date(‘截止时间’) | 格式为xxxx-xx-xx xx:xx:xx |
const left = now - preTime | 计算剩余时间 |
Math.floor(left / 60 / 60 / 24) | 时间戳获取天数 |
Math.floor(left / 60 / 60 / %20) | 获取小时数 |
Math.floor(left / 60 / %60) | 获取分钟数 |
Math.floor(left / %60) | 获取秒数 |
2. 时钟变色
定义一个1s间隔的定时器,获取随机颜色字符串,将其随定时器写入时钟样式中
const clock = document.querySelector('.clock')
const daojishi = document.querySelector('.daojishi')
let timer
timer = setInterval(function () {
const randomColor = `rgba(${Math.floor(Math.random() * (255 + 1))},${Math.floor(Math.random() * (255 + 1))},${Math.floor(Math.random() * (255 + 1))})` //随机颜色
clock.style.color = randomColor
daojishi.style.backgroundColor = randomColor
clock.innerHTML = clockTime
}, 1000)
3.当前时间
获取日期对象,用日期对象获取当前的年,月,日,时,分,秒,格式化后写入时钟中
let timer1
timer1 = setInterval(function () {
const a = new Date() //获取事件对象
const year = a.getFullYear()
const month = a.getMonth() + 1 //月份从零开始,要加1
const date = a.getDate()
const hour = a.getHours() < 10 ? '0' + a.getHours() : a.getHours() //用三位运算符实现时分秒不足10补零
const minute = a.getMinutes() < 10 ? '0' + a.getMinutes() : a.getMinutes()
const second = a.getSeconds() < 10 ? a.getSeconds() : a.getSeconds()
const clockTime = `${year}年${month}月${date}日 ${hour}:${minute}:${second}`
clock.innerHTML = clockTime
}, 1000)
4. 倒计时器
要求用户按要求输入一个字符串,根据字符串得出当前时间的时间戳和截止时间的时间戳,相减/1000得出剩余秒数,经计算得出剩余的天,月,日,时,分,秒,格式化后写入到即使中
const endInput = prompt('请输入结束时间:格式:2021-12-31 23:59:59')
let timer2
timer2 = setInterval(function () {
const startTime = +new Date() //当前时间戳
const endTime = +new Date(endInput) //截至时间时间戳
const leftTime = (endTime - startTime) / 1000
const d = Math.floor(leftTime / 60 / 60 / 24)
const h = Math.floor(leftTime / 60 / 60 % 24)
const m = Math.floor(leftTime / 60 % 60)
const s = Math.floor(leftTime % 60)
const time = `${d < 10 ? '0' + d : d}天${h < 10 ? '0' + h : h}:${m < 10 ? '0' + m : m}:${s < 10 ? '0' + s : s}`
daojishi.innerHTML = time
}, 1000)
完整实例代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 100%;
height: 500px;
}
.clock,
.daojishi {
border: 1px solid #696969;
height: 100px;
width: 400px;
margin: 100px 100px;
text-align: center;
line-height: 100px;
font-size: 30px;
font-weight: bolder;
border-radius: 20px;
}
</style>
</head>
<body>
<div class="box">
<h1>时钟:</h1>
<div class="clock"></div>
<h1>倒计时器:</h1>
<div class="daojishi"></div>
</div>
</body>
</html>
<script>
// 时钟变色
const clock = document.querySelector('.clock')
const daojishi = document.querySelector('.daojishi')
let timer
timer = setInterval(function () {
const randomColor = `rgba(${Math.floor(Math.random() * (255 + 1))},${Math.floor(Math.random() * (255 + 1))},${Math.floor(Math.random() * (255 + 1))})`
clock.style.color = randomColor
daojishi.style.backgroundColor = randomColor
}, 1000)
// 时钟
let timer1
timer1 = setInterval(function () {
const a = new Date()
const year = a.getFullYear()
const month = a.getMonth() + 1
const date = a.getDate()
const hour = a.getHours() < 10 ? '0' + a.getHours() : a.getHours()
const minute = a.getMinutes() < 10 ? '0' + a.getMinutes() : a.getMinutes()
const second = a.getSeconds() < 10 ? a.getSeconds() : a.getSeconds()
const clockTime = `${year}年${month}月${date}日 ${hour}:${minute}:${second}`
clock.innerHTML = clockTime
}, 1000)
// 倒计时器
const endInput = prompt('请输入结束时间:格式:2021-12-31 23:59:59')
let timer2
timer2 = setInterval(function () {
const startTime = +new Date()
const endTime = +new Date(endInput)
const leftTime = (endTime - startTime) / 1000
const d = Math.floor(leftTime / 60 / 60 / 24)
const h = Math.floor(leftTime / 60 / 60 % 24)
const m = Math.floor(leftTime / 60 % 60)
const s = Math.floor(leftTime % 60)
const time = `${d < 10 ? '0' + d : d}天${h < 10 ? '0' + h : h}:${m < 10 ? '0' + m : m}:${s < 10 ? '0' + s : s}`
daojishi.innerHTML = time
}, 1000)
</script>