当前位置: 首页 > news >正文

一个人生倒计时的网页应用

这是一个非常棒的想法!开发一个“人生倒计时”网页应用不仅能帮助用户直观地感受时间,也是学习HTML、CSS 和 JavaScript协同工作的经典实践。

为了确保我提供的代码完全符合你的需求,我先确认一下初步的功能设想:

1. 核心逻辑概述

该应用通常包含以下部分:

  • 输入模块:用户输入出生日期和预期寿命(例如 80 岁)。

  • 计算模块:JavaScript 计算当前时间与目标时间的差值,精确到秒甚至毫秒。

  • 展示模块:一个实时更新的数字时钟,或者一个充满格子的“人生进度条”。


2. 开发步骤规划

我将分三个阶段带你完成:

  1. 静态结构 (HTML):构建应用的骨架(标题、输入框、倒计时显示区域)。

  2. 样式美化 (CSS):让页面看起来简洁、大方,甚至带有一点哲理的静谧感。

  3. 动态逻辑 (JavaScript):编写计算函数,并设置定时器实现每秒刷新。


3. 我需要确认的细节

在开始编写完整代码前,请告诉我你的偏好:

  1. 展示风格:你希望看到的是数字倒计时(还剩多少天、多少小时),还是视觉进度条(比如 900 个格子代表 900 个月,涂满已过去的部分)?

  2. 技术栈:直接使用原生HTML/JS(无需安装,双击即可运行),还是使用像ReactVue这样的框架?(如果是初学者,建议从原生开始)。

  3. 附加功能:是否需要“保存”功能?(让用户下次打开网页时不用重新输入,这涉及浏览器的localStorage技术)。

一旦你确认了这些,我将立即为你生成完整的、可运行的代码!

代码:

<!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> /* CSS 样式:负责美化界面 */ body { font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif; background-color: #f4f4f9; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; color: #333; } .container { background: white; padding: 2rem; border-radius: 15px; box-shadow: 0 10px 30px rgba(0,0,0,0.1); text-align: center; width: 90%; max-width: 500px; } h1 { font-size: 1.5rem; margin-bottom: 1.5rem; } .input-group { margin-bottom: 1.5rem; } input { padding: 8px; border: 1px solid #ddd; border-radius: 5px; outline: none; } #countdown { font-size: 1.2rem; margin: 20px 0; line-height: 1.6; } /* 进度条容器 */ .progress-container { background: #eee; border-radius: 20px; height: 25px; width: 100%; overflow: hidden; margin-top: 10px; } /* 进度条填充部分 */ .progress-bar { background: linear-gradient(90deg, #ff7e5f, #feb47b); height: 100%; width: 0%; transition: width 0.5s ease; } .footer { font-size: 0.8rem; color: #888; margin-top: 2rem; } </style> </head> <body> <div class="container"> <h1>人生倒计时</h1> <div class="input-group"> <label>你的出生日期:</label> <input type="date" id="birthday" onChange="saveAndCalculate()"> </div> <div id="countdown">请输入生日查看倒计时</div> <div class="progress-container"> <div class="progress-bar" id="progressBar"></div> </div> <p id="percentageText" style="font-size: 0.9rem; margin-top: 5px;"></p> <div class="footer">“人生的意义在于,你在这一秒做了什么。”</div> </div> <script> // JavaScript 逻辑:负责计算和数据存储 const EXPECTED_AGE = 75; // 假设预期寿命为80岁 // 页面加载时尝试读取本地存储 window.onload = function() { const savedBirthday = localStorage.getItem('userBirthday'); if (savedBirthday) { document.getElementById('birthday').value = savedBirthday; startCountdown(); } }; function saveAndCalculate() { const birthdayVal = document.getElementById('birthday').value; if (birthdayVal) { localStorage.setItem('userBirthday', birthdayVal); startCountdown(); } } function startCountdown() { // 使用定时器,每秒刷新一次 setInterval(updateUI, 1000); updateUI(); } function updateUI() { const birthdayStr = document.getElementById('birthday').value; if (!birthdayStr) return; const birthDate = new Date(birthdayStr); const now = new Date(); const deathDate = new Date(birthDate.getFullYear() + EXPECTED_AGE, birthDate.getMonth(), birthDate.getDate()); const totalLifeMs = deathDate - birthDate; // 总寿命毫秒数 const livedMs = now - birthDate; // 已度过毫秒数 const remainingMs = deathDate - now; // 剩余毫秒数 if (remainingMs < 0) { document.getElementById('countdown').innerHTML = "你已经超越了预期的 70 岁,每一天都是额外的恩赐!"; updateProgress(100); return; } // 计算剩余的天、时、分、秒 const days = Math.floor(remainingMs / (1000 * 60 * 60 * 24)); const hours = Math.floor((remainingMs % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); const mins = Math.floor((remainingMs % (1000 * 60 * 60)) / (1000 * 60)); const secs = Math.floor((remainingMs % (1000 * 60)) / 1000); // 更新数字显示 document.getElementById('countdown').innerHTML = ` 距离 70 岁还剩:<br> <strong>${days}</strong> 天 <strong>${hours}</strong> 小时 <strong>${mins}</strong> 分 <strong>${secs}</strong> 秒 `; // 计算并更新进度条 const percent = (livedMs / totalLifeMs * 100).toFixed(2); updateProgress(percent); } function updateProgress(percent) { document.getElementById('progressBar').style.width = percent + "%"; document.getElementById('percentageText').innerText = `人生进度:${percent}%`; } </script> </body> </html>
http://www.jsqmd.com/news/629773/

相关文章:

  • Linux C并发编程基础(线程管理)
  • LFM2.5-1.2B-Thinking实战体验:Ollama部署+场景应用,提升工作效率
  • Lattice Diamond IP核配置实战:从新建项目到生成BIT文件的完整流程
  • DS1202示波器核心功能解析与实战操作指南
  • 5分钟揪出Windows热键“小偷“:Hotkey Detective终极解决方案揭秘
  • 告别云端依赖:在树莓派4B上搭建你的离线AI对话系统(Ollama + Qwen + VOSK实战)
  • Qwen3-TTS-Tokenizer-12Hz语音增强实战:修复老音频与降噪处理
  • 基于Matlab的SPEI干旱指数计算与多时间尺度nc tif数据的综合分析(2000-2023)
  • 5.2《嵌入式Linux驱动开发实战:从GPIO到UART》
  • FanControl终极指南:3步打造你的Windows风扇智能管家
  • Java ClassLoader实战:如何通过动态加密保护核心业务代码
  • 用LTspice仿真一个‘活的’线性稳压电源:拆解运放+晶体管反馈环路的每一秒
  • LocalVocal终极指南:零延迟本地字幕系统完全手册
  • 从零开始:Node.js与npm的完整安装指南(2024最新版)
  • 人不是慢慢变老的!研究发现:2个“断崖式”衰老节点,很多人没躲过
  • WeKnora效果展示:多轮对话与上下文理解能力
  • FreeRTOS 任务句柄实战指南:从创建到删除
  • 终极指南:如何安全迁移《艾尔登法环》存档并保留全部角色数据
  • 【数字IC/FPGA】从原理到实现:深入剖析移位相加乘法器的设计权衡
  • 告别臃肿:华硕笔记本性能调校的轻量化革命
  • 5.4《Linux内核驱动与应用程序交互全解析》
  • macOS 脉冲星科研环境一站式部署指南
  • 别再用Ghost了!用再生龙Clonezilla给Windows 11和Ubuntu双系统做整盘备份,保姆级避坑教程
  • 大模型推理服务混沌实验设计手册(含12类GPU/CUDA/Tokenizer层故障注入模板)
  • Sunshine终极指南:打造你的个人游戏串流服务器
  • ESPS USB MSC 调试全过程记录殉
  • nli-distilroberta-base模型原理剖析:结合计算机组成原理理解高效推理
  • 如何快速构建 macOS 现代视频播放器 IINA:完整编译指南
  • 探索Talebook个人书库:打造专属数字图书馆的完整实践
  • 保姆级教程:用Qt + OpenGL 3.3 Core Profile打造一个可交互的3D点云查看器(支持CSV导入)