首頁 web前端 css教學 建立一個年齡計算器網站

建立一個年齡計算器網站

Aug 10, 2024 am 06:44 AM

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 程式碼片段:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Age Calculator</title>
  <link href="https://fonts.googleapis.com/css?family=Manrope:200,300,regular,500,600,700,800" rel="stylesheet">
  <link rel="stylesheet" href="style.css">
  <script src="./script.js" defer></script>
</head>
<body>
  <div class="header">
    <h1>Age Calculator</h1>
  </div>

  <div class="container">
    <div class="form">
      <p id="birth">Enter your date of birth</p>
      <input type="date" id="birthday" name="birthday">
      <button id="btn">Calculate Age</button>
      <p id="result">Your age is 21 years old</p>
    </div>
  </div>

  <div class="footer">
    <p>Made with ❤️ by Abhishek Gurjar</p>
  </div>
</body>
</html>

登入後複製

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

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1677
14
CakePHP 教程
1431
52
Laravel 教程
1334
25
PHP教程
1279
29
C# 教程
1257
24
靜態表單提供商的比較 靜態表單提供商的比較 Apr 16, 2025 am 11:20 AM

讓我們嘗試在這裡造成一個術語:“靜態表單提供商”。你帶上html

每周平台新聞:HTML加載屬性,主要的ARIA規格以及從iframe轉移到Shadow dom 每周平台新聞:HTML加載屬性,主要的ARIA規格以及從iframe轉移到Shadow dom Apr 17, 2025 am 10:55 AM

在本週的平台新聞綜述中,Chrome引入了一個用於加載的新屬性,Web開發人員的可訪問性規範以及BBC Move

使Sass更快的概念證明 使Sass更快的概念證明 Apr 16, 2025 am 10:38 AM

在一個新項目開始時,Sass彙編發生在眼睛的眨眼中。感覺很棒,尤其是當它與browsersync配對時,它重新加載

帶有HTML對話框元素的一些動手 帶有HTML對話框元素的一些動手 Apr 16, 2025 am 11:33 AM

這是我第一次查看HTML元素。我已經意識到了一段時間,但是尚未將其旋轉。它很酷,

紙張形式 紙張形式 Apr 16, 2025 am 11:24 AM

購買或建造是技術的經典辯論。自己構建東西可能會感覺更便宜,因為您的信用卡賬單上沒有訂單項,但是

快速吞噬緩存破壞 快速吞噬緩存破壞 Apr 18, 2025 am 11:23 AM

您應該肯定會在CSS和JavaScript(以及圖像和字體以及其他內容)等資產上設置遙遠的高速緩存標頭。告訴瀏覽器

'訂閱播客”鏈接應在哪裡? '訂閱播客”鏈接應在哪裡? Apr 16, 2025 pm 12:04 PM

有一段時間,iTunes是播客中的大狗,因此,如果您將“訂閱播客”鏈接到喜歡:

See all articles