开发者们大家好!我很高兴分享我的最新项目:月历。该项目非常适合任何想要用 JavaScript 创建实用且具有视觉吸引力的日历的人。无论您是有兴趣向网站添加日历功能,还是只是想提高您的 JavaScript 技能,这个项目都将是您投资组合中的宝贵补充。
月历是一款基于网络的应用程序,可显示当前月份、突出显示今天的日期并准确排列一周中的日期。该项目展示了如何使用 JavaScript 动态生成日历,并结合使用 HTML 和 CSS 构建的时尚且响应式的界面。
以下是项目结构的概述:
Month-Calendar/ ├── index.html ├── style.css └── script.js
要开始该项目,请按照以下步骤操作:
克隆存储库:
git clone https://github.com/abhishekgurjar-in/Month-Calendar.git
打开项目目录:
cd Month-Calendar
运行项目:
index.html 文件定义了月历的结构,包括月份显示和日期的网格布局。这是一个片段:
8b05045a5be5764f313ed5b9168a17e6 49099650ebdc5f3125501fa170048923 93f0f5c25f18dab9d176bd4f6de5d30e 7c8d9f814bcad6a1d7abe4eda5f773e5 acd8feeb3a0ea7477b979779de32785a 26faf3d1af674280d03ba217d87e9421 b2386ffb911b14667cb8f0f91ea547a7Month Calendar6e916e0f7d1e588d4f442bf645aedb2f af75c476cdb7e6c074ca6da9b40841de 90392ec4442ad9ff612213ec639da4832cacc6d41bbb37262a98f745aa00fbf0 9c3bca370b5104690d9ef395f2c5f8d1 6c04bd5ca3fcae76e30b72ad730ca86d 4883ec0eb33c31828b7c767c806e14c7 924ff17625d603f964501dd897c96cc6 4a249f0d628e2318394fd9b75b4636b1Month Calendar473f0a7621bec819994bb5020d29372a 16b28748ea4df4d9c2150843fecfba68 6a0cba8356a55c4a6e47746d431e6421 418883615a2e79b9926c86666863ce0d 5f41073e18caf0aa09116baf33d01f47 4a249f0d628e2318394fd9b75b4636b1473f0a7621bec819994bb5020d29372a e388a4556c0f65e1904146cc1a846bee94b3e26ee717c64999d7867364b1b4a3 16b28748ea4df4d9c2150843fecfba68 16b28748ea4df4d9c2150843fecfba68 639769261d891863793f213a2da06288 dc6dce4a544fdca2df29d5ac0ea9906bMon16b28748ea4df4d9c2150843fecfba68 dc6dce4a544fdca2df29d5ac0ea9906bTue16b28748ea4df4d9c2150843fecfba68 dc6dce4a544fdca2df29d5ac0ea9906bWed16b28748ea4df4d9c2150843fecfba68 dc6dce4a544fdca2df29d5ac0ea9906bThu16b28748ea4df4d9c2150843fecfba68 dc6dce4a544fdca2df29d5ac0ea9906bFri16b28748ea4df4d9c2150843fecfba68 dc6dce4a544fdca2df29d5ac0ea9906bSat16b28748ea4df4d9c2150843fecfba68 dc6dce4a544fdca2df29d5ac0ea9906bSun16b28748ea4df4d9c2150843fecfba68 16b28748ea4df4d9c2150843fecfba68 97a12517d2546b2575274c1d8a6e171e16b28748ea4df4d9c2150843fecfba68 16b28748ea4df4d9c2150843fecfba68 ffd6ba4147bda351239915f463e46e38 e388a4556c0f65e1904146cc1a846beeMade with ❤️ by Abhishek Gurjar94b3e26ee717c64999d7867364b1b4a3 16b28748ea4df4d9c2150843fecfba68 16b28748ea4df4d9c2150843fecfba68 36cc49f0c466276486e50c850b7e4956 73a6ac4ed44ffec12cee46588e518a5e
style.css 文件设置月历的样式,使其既有吸引力又响应灵敏。以下是一些关键样式:
* { margin: 0; padding: 0; font-family: sans-serif; box-sizing: border-box; } .container { width: 100%; height: 100vh; background-color: salmon; display: flex; justify-content: center; align-items: center; flex-direction: column; } .header { margin: 20px; color: white; text-align: center; } .calendar { background-color: black; color: lightgray; width: 450px; height: 520px; border-radius: 10px; box-shadow: 4px 4px 8px rgba(0, 0, 0, 0.4); } .month { width: 100%; height: 120px; background-color: lightseagreen; display: flex; justify-content: center; align-items: center; text-align: center; border-radius: 10px 10px 0 0; } .month h1 { font-size: 30px; font-weight: 400; text-transform: uppercase; } .month p { font-size: 16px; } .weekdays { width: 100%; height: 50px; display: flex; } .weekdays div { font-size: 15px; font-weight: bold; letter-spacing: 1px; width: 100%; display: flex; align-items: center; justify-content: center; } .days { width: 100%; display: flex; flex-wrap: wrap; padding: 2px; } .days div { font-size: 14px; margin: 3px; width: 57.5px; height: 50px; display: flex; justify-content: center; align-items: center; } .days div:hover:not(.empty) { border: 2px solid gray; cursor: pointer; } .today { background-color: lightseagreen; } .footer { margin-top: 70px; color: white; text-align: center; }
script.js 文件包含生成日历和突出显示当前日期的逻辑。这是一个片段:
const monthEl = document.querySelector(".date h1"); const fullDateEl = document.querySelector(".date p"); const daysEl = document.querySelector(".days"); const monthInx = new Date().getMonth(); const lastDay = new Date(new Date().getFullYear(), monthInx + 1, 0).getDate(); const firstDay = new Date(new Date().getFullYear(), monthInx, 1).getDay() - 1; const months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", ]; monthEl.innerText = months[monthInx]; fullDateEl.innerText = new Date().toDateString(); let days = ""; for (let i = firstDay; i > 0; i--) { days += `edfd5fc8d5ee204ac4dfe03b945f3c3916b28748ea4df4d9c2150843fecfba68`; } for (let i = 1; i 67a68beefb344083e30784f1b3a9b000${i}16b28748ea4df4d9c2150843fecfba68`; } else { days += `dc6dce4a544fdca2df29d5ac0ea9906b${i}16b28748ea4df4d9c2150843fecfba68`; } } daysEl.innerHTML = days;
您可以在此处查看月历项目的现场演示。
创建月历是一个令人愉快的项目,它使我能够将我的前端开发技能与实用的 JavaScript 功能结合起来。该工具对于在网站上显示日历日期非常有用,并且可以成为您的 Web 开发项目的一个很好的补充。我希望你发现它和我一样有帮助。快乐编码!
这个项目是我增强 JavaScript 技能和创建功能性 Web 工具之旅的一部分。
以上是建立一个月历网站的详细内容。更多信息请关注PHP中文网其他相关文章!