首頁 web前端 css教學 建立一個溫度轉換器網站

建立一個溫度轉換器網站

Aug 19, 2024 am 04:28 AM

Build a Temperature Converter Website

介紹

各位開發者大家好!我很高興分享我的最新項目:實用的溫度轉換器。該專案非常適合希望透過處理使用者輸入、執行轉換和動態更新 DOM 來增強 JavaScript 技能的人。無論您是初學者還是經驗豐富的開發人員,此溫度轉換器都是了解單位轉換基礎知識的絕佳項目。

項目概況

溫度轉換器是一個基於網路的應用程序,允許用戶輕鬆地在攝氏度、華氏度和開爾文之間轉換溫度。此專案示範如何建立互動式使用者介面、處理計算並向使用者提供即時回饋。

特徵

  • 使用者友善的介面:簡單直覺的設計,易於使用。
  • 即時轉換:輸入溫度值時立即轉換。
  • 響應式設計:佈局適應不同的螢幕尺寸,在桌面和行動裝置上提供無縫體驗。
  • 多單位支援:在攝氏度、華氏度和開爾文之間轉換。

使用的技術

  • HTML:建立網頁和輸入元素。
  • CSS:設定介面樣式,確保簡潔且響應靈敏的設計。
  • JavaScript:處理轉換邏輯並即時更新溫度值。

專案結構

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

Temperature-Converter/
├── index.html
├── styles.css
└── script.js
  • index.html:包含溫度轉換器的 HTML 結構。
  • styles.css:包含 CSS 樣式以增強轉換器的外觀。
  • script.js:管理轉換邏輯與動態更新。

安裝

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

  1. 複製儲存庫

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

    cd Temperature-Converter
    
  3. 運行項目:

    • 在網頁瀏覽器中開啟index.html 檔案以開始使用溫度轉換器。

用法

  1. 在網頁瀏覽器中開啟網站
  2. 在攝氏度、華氏度或開爾文輸入欄位中輸入溫度值
  3. 查看轉換後的值在對應欄位中自動更新。
  4. 如果您想開始新的轉換,請重設欄位

程式碼說明

超文本標記語言

index.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>Temperature Converter</title>
    <link rel="stylesheet" href="./style.css" />
    <script src="./script.js" defer></script>
  </head>
  <body>
    <video id="background-video" autoplay loop muted poster="https://assets.codepen.io/6093409/river.jpg">
      <source src="./images/bg.mp4" type="video/mp4">
    </video>
    <div class="container">
      <h1 class="heading">Temperature Converter</h1>
      <div class="temp-container">
        <label for="celsius">Celsius:</label>
        <input
          onchange="computeTemp(event)"
          type="number"
          name="celsius"
          id="celsius"
          placeholder="Enter Temperature"
          class="input"
        />
      </div>
      <div class="temp-container">
        <label for="fahrenheit">Fahrenheit:</label>
        <input
          onchange="computeTemp(event)"
          type="number"
          name="fahrenheit"
          id="fahrenheit"
          placeholder="Enter Temperature"
          class="input"
        />
      </div>
      <div class="temp-container">
        <label for="kelvin">Kelvin:</label>
        <input
          onchange="computeTemp(event)"
          type="number"
          name="kelvin"
          id="kelvin"
          placeholder="Enter Temperature"
          class="input"
        />
      </div>
    </div>
    <div class="footer">
      <p>Made with ❤️ by Abhishek Gurjar</p>
    </div>
  </body>
</html>

CSS

styles.css 檔案設定溫度轉換器的樣式,提供乾淨且響應式的佈局。以下是一些關鍵樣式:

body {
  margin: 0;
  background: url(./images/bg.mp4);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  font-family: monospace;
  color: white;
}

.container {
  background: #202124;
  padding: 20px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  border-radius: 10px;
  width: 85%;
  max-width: 450px;
  min-width: 350px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.heading {
  font-size: 32px;
}

.temp-container {
  width: 100%;
  padding: 15px;
  font-weight: bold;
  font-size: 18px;
}

.input {
  width: 220px;
  font-family: monospace;
  padding: 5px;
  float: right;
  outline: none;
  background: white;
  border-color: white;
  border-radius: 5px;
  color: black;
  font-size: 18px;
}

.input::placeholder {
  color: gray;
}

#background-video {
  width: 100vw;
  height: 100vh;
  object-fit: cover;
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  z-index: -1;
}

.footer {
  margin-top: 200px;
  text-align: center;
}

JavaScript

script.js 檔案處理轉換邏輯,依照使用者輸入更新溫度值。這是一個片段:

const celsiusEl = document.getElementById("celsius");
const fahrenheitEl = document.getElementById("fahrenheit");
const kelvinEl = document.getElementById("kelvin");

function computeTemp(event) {
  const currentValue = +event.target.value;

  switch (event.target.name) {
    case "celsius":
      kelvinEl.value = (currentValue + 273.32).toFixed(2);
      fahrenheitEl.value = (currentValue * 1.8 + 32).toFixed(2);
      break;
    case "fahrenheit":
      celsiusEl.value = ((currentValue - 32) / 1.8).toFixed(2);
      kelvinEl.value = ((currentValue - 32) / 1.8 + 273.32).toFixed(2);
      break;
    case "kelvin":
      celsiusEl.value = (currentValue - 273.32).toFixed(2);
      fahrenheitEl.value = ((currentValue - 273.32) * 1.8 + 32).toFixed(2);
      break;
    default:
      break;
  }
}

現場演示

您可以在此處查看溫度轉換器的現場示範。

結論

建立這個溫度轉換器是一次有益的經歷,它增強了我對 JavaScript 以及如何建立互動式 Web 應用程式的理解。我希望這個專案能夠激勵您進一步探索並建立自己的轉換工具。快樂編碼!

製作人員

這個專案是我不斷提升 Web 開發技能的一部分,專注於 JavaScript 和 DOM 操作。

作者

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

以上是建立一個溫度轉換器網站的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

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)

什麼是'渲染障礙CSS”? 什麼是'渲染障礙CSS”? Jun 24, 2025 am 12:42 AM

CSS會阻塞頁面渲染是因為瀏覽器默認將內聯和外部CSS視為關鍵資源,尤其是使用引入的樣式表、頭部大量內聯CSS以及未優化的媒體查詢樣式。 1.提取關鍵CSS並內嵌至HTML;2.延遲加載非關鍵CSS通過JavaScript;3.使用media屬性優化加載如打印樣式;4.壓縮合併CSS減少請求。建議使用工具提取關鍵CSS,結合rel="preload"異步加載,合理使用media延遲加載,避免過度拆分與復雜腳本控制。

什麼是AutoPrefixer,它如何工作? 什麼是AutoPrefixer,它如何工作? Jul 02, 2025 am 01:15 AM

Autoprefixer是一個根據目標瀏覽器範圍自動為CSS屬性添加廠商前綴的工具。 1.它解決了手動維護前綴易出錯的問題;2.通過PostCSS插件形式工作,解析CSS、分析需加前綴的屬性、依配置生成代碼;3.使用步驟包括安裝插件、設置browserslist、在構建流程中啟用;4.注意事項有不手動加前綴、保持配置更新、非所有屬性都加前綴、建議配合預處理器使用。

什麼是圓錐級函數? 什麼是圓錐級函數? Jul 01, 2025 am 01:16 AM

theconic-Gradient()functionIncsscreatesCircularGradientsThatRotateColorStopSaroundAcentralPoint.1.IsidealForPieCharts,ProgressIndicators,colordichers,colorwheels和decorativeBackgrounds.2.itworksbysbysbysbydefindefingincolordefingincolorstopsatspecificains off.

CSS教程,用於創建粘性標頭或頁腳 CSS教程,用於創建粘性標頭或頁腳 Jul 02, 2025 am 01:04 AM

TocreatestickyheadersandfooterswithCSS,useposition:stickyforheaderswithtopvalueandz-index,ensuringparentcontainersdon’trestrictit.1.Forstickyheaders:setposition:sticky,top:0,z-index,andbackgroundcolor.2.Forstickyfooters,betteruseposition:fixedwithbot

CSS自定義屬性的範圍是什麼? CSS自定義屬性的範圍是什麼? Jun 25, 2025 am 12:16 AM

CSS自定義屬性的作用域取決於其聲明的上下文,全局變量通常定義在:root中,而局部變量則定義在特定選擇器內,以便組件化和隔離樣式。例如,定義在.card類中的變量僅對匹配該類的元素及其子元素可用。最佳實踐包括:1.使用:root定義全局變量如主題色;2.在組件內部定義局部變量以實現封裝;3.避免重複聲明同一變量;4.注意選擇器特異性可能引發的覆蓋問題。此外,CSS變量區分大小寫,且應在使用前定義以避免錯誤。若變量未定義或引用失敗,則會採用回退值或默認值initial。調試時可通過瀏覽器開發者工

CSS網格中的FR單元是什麼? CSS網格中的FR單元是什麼? Jun 22, 2025 am 12:46 AM

ThefrunitinCSSGriddistributesavailablespaceproportionally.1.Itworksbydividingspacebasedonthesumoffrvalues,e.g.,1fr2frgivesone-thirdandtwo-thirds.2.Itenablesflexiblelayouts,avoidsmanualcalculations,andsupportsresponsivedesign.3.Commonusesincludeequal-

CSS教程專注於移動優先設計 CSS教程專注於移動優先設計 Jul 02, 2025 am 12:52 AM

Mobile-firstCSSdesignrequiressettingtheviewportmetatag,usingrelativeunits,stylingfromsmallscreensup,optimizingtypographyandtouchtargets.First,addtocontrolscaling.Second,use%,em,orreminsteadofpixelsforflexiblelayouts.Third,writebasestylesformobile,the

您可以在CSS網格項目中嵌套Flexbox容器嗎? 您可以在CSS網格項目中嵌套Flexbox容器嗎? Jun 22, 2025 am 12:40 AM

是的,可以在CSSGrid項中使用Flexbox。具體做法是先用Grid劃分頁面結構,在某個Grid單元格內設置子容器為Flex容器,以實現更精細的對齊和排列;例如,在HTML中嵌套一個帶有display:flex樣式的div;這樣做的好處包括分層佈局、響應式設計更容易、組件化開發更友好;需要注意display屬性僅影響直接子元素、避免過度嵌套、考慮舊版瀏覽器兼容性問題。

See all articles