沈阳做网站seo广告公司名字
博主主页:技术分享菌
博主简介:专注于前端开发领域,旨在帮助初学者提升前端技能水平,分享技术资讯和实用教程。内容涵盖 HTML、CSS、JavaScript、前端框架(如 React、Vue、Angular 等)以及前端工程化等方面。我们将不断更新优质内容,为大家提供一场前端技术的盛宴,欢迎你的关注和参与。
文章目录
前言
一、倒计时 倒数日是什么?
二、功能实现
(一)、倒数日
1. 效果图展示
2. HTML 部分
3. CSS 部分
4. JS 部分
5. 整体代码
(二)、倒计时
1.效果图展示
2. HTML部分
3. CSS部分
4. JS部分
5. 整体代码
总结
前言
在日常生活和各种场景中,倒数日、倒计时可以帮助我们更好地规划时间、提高工作效率、激发紧迫感等。在互联网领域,倒数日功能也被广泛应用于各类倒计时应用,如:活动促销、比赛倒计时、课程预约等。
一、倒计时 倒数日是什么?
倒数日、倒计时(Countdown)是一种常见的时间展示方式,用于提醒用户某个重要事件即将到来。在日常生活和各种场景中,倒计时可以帮助人们更好地规划时间、提高工作效率、激发紧迫感等。随着互联网技术的发展,倒计时功能在网页设计中变得越来越常见,如:活动促销、比赛倒计时、课程预约等。
二、功能实现
(一)、倒数日
1. 效果图展示
2. HTML 部分
代码如下(示例):
<!DOCTYPE html>
<html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>倒数日</title>
</head><body><div class="countdown"><div class="title">距离<span>春节</span>还有</div><div class="day"><strong id="days">5000</strong> <span>天</span></div><div class="target">目标日:<span id="goal">2025-01-01</span></div></div>
</body></html>
3. CSS 部分
代码如下(示例):
* {margin: 0;padding: 0;}body {background: -webkit-linear-gradient(to right, #FFFDE4, #005AA7);background: linear-gradient(to right, #FFFDE4, #005AA7);}.countdown {margin: 50px auto;width: 400px;height: 450px;background-color: rgb(145, 3, 3);background-image: url(./festival.jpg);background-size: cover;}.countdown .title {width: 100%;height: 100px;text-align: center;line-height: 100px;font-size: 24px;color: white;}.countdown .day {width: 100%;height: 250px;line-height: 250px;text-align: center;color: white;}.countdown .day strong {display: inline-block;padding: 15px;height: 100px;line-height: 100px;background-color: black;font-size: 72px;}.countdown .day span {font-size: 36px;}.countdown .target {height: 100px;line-height: 100px;text-align: center;color: white;}
对CSS部分代码进行说明解释
countdown类中 我写 背景颜色 和 背景图,你们拿到代码就只显示背景色
如需要背景图,点击百度网盘链接:网页图片资源
4. JS 部分
// 函数封装 getCountTimefunction getCountTime() {// 1. 获得当前时间戳const now = +new Date()// 2. 得到将来的时间戳const time = '2024-01-01' //目标时间修改处const after = +new Date(time)// console.log(now,after);// 时间戳是毫秒// 3. 将得到剩余的时间戳 sec 记得转换成 秒数const sec = (after - now) / 1000// 4. 转换为 天数cosnt d = parseInt(sec / 60 / 60 / 24) // 计算天数// console.log(h,m,s);document.querySelector('.title span').innerHTML = '元旦' //修改标题处document.querySelector('#days').innerHTML = ddocument.querySelector('#goal').innerHTML = time}// 先调用一次getCountTime()// 开启定时器setInterval(getCountTime, 1000)
对JS部分代码进行说明解释
如需要修改目标时间 、标题,找到对应的注释代码
时间戳是毫秒,需要转换为秒数 公式: 秒数 = 毫秒 / 1000
计算天数 公式
cosnt d = parseInt(总秒数 / 60 / 60 / 24)
5. 整体代码
<!DOCTYPE html>
<html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>倒数日</title><style>* {margin: 0;padding: 0;}body {background: -webkit-linear-gradient(to right, #FFFDE4, #005AA7);background: linear-gradient(to right, #FFFDE4, #005AA7);}.countdown {margin: 50px auto;width: 400px;height: 450px;background-color: rgb(145, 3, 3);background-image: url(./festival.jpg);background-size: cover;}.countdown .title {width: 100%;height: 100px;text-align: center;line-height: 100px;font-size: 24px;color: white;}.countdown .day {width: 100%;height: 250px;line-height: 250px;text-align: center;color: white;}.countdown .day strong {display: inline-block;padding: 15px;height: 100px;line-height: 100px;background-color: black;font-size: 72px;}.countdown .day span {font-size: 36px;}.countdown .target {height: 100px;line-height: 100px;text-align: center;color: white;}</style>
</head><body><div class="countdown"><div class="title">距离<span>春节</span>还有</div><div class="day"><strong id="days">5000</strong> <span>天</span></div><div class="target">目标日:<span id="goal">2025-01-01</span></div></div><script>// 函数封装 getCountTimefunction getCountTime() {// 1. 获得当前时间戳const now = +new Date()// 2. 得到将来的时间戳const time = '2024-01-01' //目标时间修改处const after = +new Date(time)// console.log(now,after);// 3. 将得到剩余的时间戳 sec 记得转换成 秒数const sec = (after - now) / 1000// 4. 转换为 天数const d = parseInt(sec / 60 / 60 / 24) // 计算天数// console.log(h,m,s);document.querySelector('.title span').innerHTML = '元旦' //修改标题处document.querySelector('#days').innerHTML = ddocument.querySelector('#goal').innerHTML = time}// 先调用一次getCountTime()// 开启定时器setInterval(getCountTime, 1000)</script>
</body></html>
(二)、倒计时
1.效果图展示
图1 实际效果图 图2 时间过去后效果图
2. HTML 部分
代码如下(示例):
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>倒计时</title>
</head><body><div class="countdown"><p class="next">今天是2023年11月11日</p><p class="title">下班倒计时</p><p class="clock"><span id="hour">00</span><i>:</i><span id="minutes">25</span><i>:</i><span id="scond">20</span></p><p class="tips"><span>23:30:00</span> 下班</p></div>
</body></html>
3. CSS 部分
.countdown {width: 240px;height: 305px;text-align: center;line-height: 1;color: #fff;background-color: brown;overflow: hidden;}.countdown .next {font-size: 16px;margin: 25px 0 14px;}.countdown .title {font-size: 33px;}.countdown .tips {margin-top: 80px;font-size: 22px;}.countdown small {font-size: 17px;}.countdown .clock {width: 142px;margin: 18px auto 0;overflow: hidden;}.countdown .clock span,.countdown .clock i {display: block;text-align: center;line-height: 34px;font-size: 23px;float: left;}.countdown .clock span {width: 34px;height: 34px;border-radius: 2px;background-color: #303430;}.countdown .clock i {width: 20px;font-style: normal;}
4. JS 部分
// 日期函数封装 getMyDateconst today = document.querySelector('.countdown .next')function getMyDate() {const date = new Date()return `今天是 ${date.getFullYear()} 年 ${date.getMonth() + 1} 月 ${date.getDate()} 日`}today.innerHTML = getMyDate()// 函数封装 getCountTimefunction getCountTime() {// 1. 获得当前时间const now = +new Date()// 2. 得到将来的时间戳const time = '21:30' // 修改时间处const after = +new Date('2050-11-15 '+time) //修改日期处// console.log(now,after);// 3. 将得到剩余的时间戳 sec 记得转换成 秒数const sec = (after - now) / 1000// 4. 转换为 时分秒let h = parseInt(sec / 60 / 60 % 24)let m = parseInt(sec / 60 % 60)let s = parseInt(sec % 60)// 当小于10 需要补0h = h < 10 ? '0' + h : hm = m < 10 ? '0' + m : ms = s < 10 ? '0' + s : s// console.log(h,m,s);document.querySelector('#hour').innerHTML = hdocument.querySelector('#minutes').innerHTML = mdocument.querySelector('#scond').innerHTML = sdocument.querySelector('.tips span').innerHTML = time}// 先调用一次getCountTime()// 开启定时器setInterval(getCountTime, 1000)
对JS部分代码进行说明解释
如需要修改时间 、日期,找到对应的注释代码
倒计时核心代码 封装 getCountTime 函数
时间戳是毫秒,需要转换为秒数 公式: 秒数 = 毫秒 / 1000
计算时、分、秒 公式
h = parseInt(总秒数 / 60 / 60 % 24) // 计算小时
m = parseInt(总秒数 / 60 % 60); // 计算分数
s = parseInt(总秒数 % 60); // 计算秒数
时分秒 需要进行判断操作
用简洁的三元运算符进行判断 如小于10 就需要进行补0 字符串拼接操作
5. 整体代码
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>倒计时</title><style>.countdown {width: 240px;height: 305px;text-align: center;line-height: 1;color: #fff;background-color: brown;overflow: hidden;}.countdown .next {font-size: 16px;margin: 25px 0 14px;}.countdown .title {font-size: 33px;}.countdown .tips {margin-top: 80px;font-size: 22px;}.countdown small {font-size: 17px;}.countdown .clock {width: 142px;margin: 18px auto 0;overflow: hidden;}.countdown .clock span,.countdown .clock i {display: block;text-align: center;line-height: 34px;font-size: 23px;float: left;}.countdown .clock span {width: 34px;height: 34px;border-radius: 2px;background-color: #303430;}.countdown .clock i {width: 20px;font-style: normal;}</style>
</head><body><div class="countdown"><p class="next">今天是2023年11月15日</p><p class="title">下班倒计时</p><p class="clock"><span id="hour">00</span><i>:</i><span id="minutes">25</span><i>:</i><span id="scond">20</span></p><p class="tips"><span>23:30:00</span> 下班</p></div><script>// 日期函数封装 getMyDateconst today = document.querySelector('.countdown .next')function getMyDate() {const date = new Date()return `今天是 ${date.getFullYear()} 年 ${date.getMonth() + 1} 月 ${date.getDate()} 日`}today.innerHTML = getMyDate()// 函数封装 getCountTimefunction getCountTime() {// 1. 获得当前时间const now = +new Date()// 2. 得到将来的时间戳const time = '21:30' // 修改时间日期处const after = +new Date('2050-11-15 '+time)// console.log(now,after);// 3. 将得到剩余的时间戳 sec 记得转换成 秒数const sec = (after - now) / 1000// 4. 转换为 时分秒let h = parseInt(sec / 60 / 60 % 24)let m = parseInt(sec / 60 % 60)let s = parseInt(sec % 60)// 当小于10 需要补0h = h < 10 ? '0' + h : hm = m < 10 ? '0' + m : ms = s < 10 ? '0' + s : s// console.log(h,m,s);document.querySelector('#hour').innerHTML = hdocument.querySelector('#minutes').innerHTML = mdocument.querySelector('#scond').innerHTML = sdocument.querySelector('.tips span').innerHTML = time}// 先调用一次getCountTime()// 开启定时器setInterval(getCountTime, 1000)</script>
</body></html>
总结
以上就是今天要讲的内容,本文仅仅简单介绍了日期对象,时间戳在实际开发使用,而Date对象提供了大量能使我们快速处理数据的方法。在日常生活和各种场景中,倒计时,倒数日可以帮助人们更好地规划时间、提高工作效率、激发紧迫感等。随着互联网技术的发展,倒计时功能在网页设计中变得越来越常见。