首頁  >  文章  >  web前端  >  建立一個年齡計算器網站

建立一個年齡計算器網站

王林
王林原創
2024-08-10 06:44:03867瀏覽

Building a Age Calculator Website

介紹

各位開發者大家好!今天,我很高興分享我最近完成的一個項目:年齡計算器。該項目允許用戶根據出生日期計算準確的年齡,並在清晰且用戶友好的介面中提供結果。這是練習使用 JavaScript(尤其是日期和時間函數)同時建立實用內容的好方法。

項目概況

年齡計算器旨在為使用者提供一種簡單的方法來了解他們當前的年齡(以年、月和日為單位)。使用者只需輸入他們的出生日期,然後點擊按鈕,就會顯示他們的年齡。該專案非常適合想要提高處理日期和建立互動式 Web 應用程式技能的開發人員。

特徵

  • 日期輸入:使用者可以使用日期選擇器輸入出生日期,以便準確輸入。
  • 年齡計算:應用程式計算使用者的確切年齡,精確到天數。
  • 響應式設計:此工具設計為響應式,確保它在不同的裝置和螢幕尺寸上順利運作。

使用的技術

  • HTML:用來建立網頁上的內容。
  • CSS:用於設計網頁樣式並確保其在各種裝置上回應。
  • JavaScript:實作用於處理年齡計算邏輯和更新 DOM。

專案結構

以下是專案結構的快速瀏覽:

Age-Calculator/
├── index.html
├── style.css
└── script.js
  • index.html:包含網頁的 HTML 結構。
  • style.css:儲存 CSS 樣式,包括響應式設計規則。
  • script.js:使用 JavaScript 管理頁面的動態方面。

安裝

要開始該項目,請按照以下步驟操作:

  1. 複製儲存庫

    git clone https://github.com/abhishekgurjar-in/Age-Calculator.git
    
  2. 開啟專案目錄:

    cd Age-Calculator
    
  3. 運行項目:

    • 您可以在本機伺服器上運行它,也可以簡單地在網頁瀏覽器中開啟index.html 檔案。

用法

  1. 在網頁瀏覽器中開啟網站
  2. 使用日期選擇器輸入您的出生日期
  3. 點擊「計算年齡」按鈕即可查看螢幕上顯示的您的確切年齡。

程式碼說明

超文本標記語言

index.html 檔案包含網頁的結構,包括輸入表單和顯示計算出的年齡的部分。以下是 HTML 程式碼片段:

8b05045a5be5764f313ed5b9168a17e6
49099650ebdc5f3125501fa170048923
93f0f5c25f18dab9d176bd4f6de5d30e
  1fc2df4564f5324148703df3b6ed50c1
  4f2fb0231f24e8aef524fc9bf9b9874f
  b2386ffb911b14667cb8f0f91ea547a7Age Calculator6e916e0f7d1e588d4f442bf645aedb2f
  097655f07349bae5bc1e68fdb79ed306
  468dcf7b0ee61aef03af1a1fbe6725fc
  5de102113aede4703971b3b780c58efb2cacc6d41bbb37262a98f745aa00fbf0
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
  924ff17625d603f964501dd897c96cc6
    4a249f0d628e2318394fd9b75b4636b1Age Calculator473f0a7621bec819994bb5020d29372a
  16b28748ea4df4d9c2150843fecfba68

  4883ec0eb33c31828b7c767c806e14c7
    6aee8d1285fa83b7a99788e2e8734436
      2b836c77cbbab63c3f9e4b62c3ecc02aEnter your date of birth94b3e26ee717c64999d7867364b1b4a3
      53fcc26495096764d98f5e40e6cb0a78
      0daa849bc639fff17b006ca8cf3a07caCalculate Age65281c5ac262bf6d81768915a4a77ac0
      9369b95c0a54fa14078c71ff87b92a53Your age is 21 years old94b3e26ee717c64999d7867364b1b4a3
    16b28748ea4df4d9c2150843fecfba68
  16b28748ea4df4d9c2150843fecfba68

  ffd6ba4147bda351239915f463e46e38
    e388a4556c0f65e1904146cc1a846beeMade with ❤️ by Abhishek Gurjar94b3e26ee717c64999d7867364b1b4a3
  16b28748ea4df4d9c2150843fecfba68
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

CSS

style.css 檔案包含確保網頁具有視覺吸引力和回應能力的樣式。以下是一些關鍵樣式:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: "Manrope", sans-serif;
  width: 100%;
  height: 100vh;
  background: #2962ff;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: white;
}

.header {
  margin-bottom: 80px;
  text-align: center;
}
.container {
  background: black;
  color: white;
  width: 600px;
  height: 300px;
  border-radius: 5px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;
}

.form {
  display: flex;
  flex-direction: column;
  align-items: center;
}

p {
  font-weight: bold;
  margin: 20px;
}

input {
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 5px;
  width: 100%;
  max-width: 300px;
}

button {
  background-color: #007bff;
  color: white;
  border: none;
  margin: 20px;
  padding: 10px 20px;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

button:hover {
  background-color: #0062cc;
}

#result {
  margin-top: 20px;
  font-size: 24px;
  font-weight: bold;
}


.footer {
  margin: 70px;
  text-align: center;
}
.footer p{
  font-size: 14px;
  font-weight: 400;
}

JavaScript

script.js 檔案管理年齡計算邏輯並在網頁上更新結果。以下是 JavaScript 程式碼片段:

const btnE1 = document.getElementById("btn");
const birthE1 = document.getElementById("birthday");
const resultE1 = document.getElementById("result");

function calculateAge() {
  const birthdayValue = birthE1.value;
  if (birthdayValue === "") {
    alert("Please enter your birthday");
  } else {
    const age = getAge(birthdayValue);
    resultE1.innerText = `Your age is ${age} ${age > 1 ? "years" : "year"} old.`;
  }
}

function getAge(birthdayValue) {
  const birthdayDate = new Date(birthdayValue);
  const currentDate = new Date();
  let age = currentDate.getFullYear() - birthdayDate.getFullYear();
  const month = currentDate.getMonth() - birthdayDate.getMonth();

  if (
    month < 0 ||
    (month === 0 && currentDate.getDate() < birthdayDate.getDate())
  ) {
    age--;
  }

  return age;
}

btnE1.addEventListener("click", calculateAge);

現場演示

您可以在此處查看年齡計算器的現場演示。

結論

建立這個年齡計算器是一次有益的經歷,它讓我加深了對使用日期和建立互動式 Web 應用程式的理解。我希望您發現這個項目對您自己的學習之旅有用且富有洞察力。請隨意探索程式碼並根據您的需求進行調整。快樂編碼!

製作人員

這個項目的靈感來自於對簡單有效的年齡計算工具的需求。

作者

  • 阿布舍克·古賈爾
    • GitHub 簡介

以上是建立一個年齡計算器網站的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn