重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
這篇文章主要為大家展示了“怎么用JS和API制作天氣Web應用程序”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“怎么用JS和API制作天氣Web應用程序”這篇文章吧。
創新互聯專業為企業提供蘭陵網站建設、蘭陵做網站、蘭陵網站設計、蘭陵網站制作等企業網站建設、網頁設計與制作、蘭陵企業網站模板建站服務,10年蘭陵做網站經驗,不只是建網站,更提供有價值的思路和整體網絡服務。
使用您喜歡的代碼編輯器,創建一個名為“Weather App
”或任何您想要的名字,然后創建這三個文件并將這些資源添加到文件夾中:
index.html
style.css
script.js
我們需要的其他資源:
Favicon
Loading GIF (optional)
Vanilla-Tilt.js file
下載所有這些資源地址:https://download.csdn.net/download/qq_44273429/20463321
從HTML 文件的常用模板開始。根據需要添加標題。
在鏈接style.css
和之前script.js
,鏈接您想要的谷歌字體。我使用過Poppins
字體,這是我比較喜歡的字體之一。(谷歌字體)
HTML
現在從body
開始,如果您希望向您的網站添加加載程序,那么您可以將其添加到正文標簽中,然后為其編寫腳本。
HTML
制作兩個單獨的div。一個用于heading title
,一個用于卡片。在它下面添加合適的div標簽。
這里我使用了一個SVG
格式的搜索按鈕。您可以將此代碼用于卡片div
中的按鈕。
HTML
為默認圖標顯示添加天氣圖標。
HTML
多云
加載動畫和Vanilla-Tilt js
的腳本。在正文結束之前添加它。我在上面步驟 1 中提到的資源中添加了Vanilla-Tilt Js
文件。
JS
從樣式body
和其他元素開始。
設置加載動畫的樣式。您可以使用此代碼對其進行樣式設置。由于加載動畫具有白色背景,因此我使用了#fff。
我在資源文件夾中添加了SVG
圖像。
CSS
#loading{ position: fixed; width: 100%; height: 100vh; background: #fff url('/loading.svg') no-repeat center; z-index: 99999; }
請參閱Github存儲庫以獲取 CSS 代碼
地址:https://github.com/wanghao221/Weather.io
前往OpenWeatherMap
并創建一個帳戶。登錄后單擊API Keys
選項卡中的 ,您將看到API
密鑰。復制API Key
并粘貼到下面提到的 JavaScript
代碼的第二行 (apiKey: "
)
前往
Unsplash Source
。在這里,您可以看到如何根據大小、文本、用戶的喜好、收藏等以不同的方式調用圖片。
在JavaScipt
中集成API
對于學習如何為Web
應用程序使用API
至關重要。我已經列出了整個代碼。你可以通過它并理解代碼。
我已將此調用"url('https://source.unsplash.com/1600x900/?city " + name + "'
)"用于背景圖像。您可以根據需要自定義URL
。
我還使用了上海市的默認天氣weather.fetchWeather("Shanghai");
。您可以在此處添加任何城市的名稱。每當您加載網站時,都會彈出這個城市的天氣。
JS
let weather = { apiKey: "", fetchWeather: function (city) { fetch( "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=metric&appid=" + this.apiKey ) .then((response) => response.json()) .then((data) => this.displayWeather(data)); }, displayWeather: function (data) { const { name } = data; const { icon, description } = data.weather[0]; const { temp, humidity } = data.main; const { speed } = data.wind; document.querySelector(".city").innerText = "Weather in " + name; document.querySelector(".icon").src = "https://openweathermap.org/img/wn/" + icon + ".png"; document.querySelector(".description").innerText = description; document.querySelector(".temp").innerText = temp + "°C"; document.querySelector(".humidity").innerText = "濕度: " + humidity + "%"; document.querySelector(".wind").innerText = "風速: " + speed + " km/h"; document.querySelector(".weather").classList.remove("loading"); document.body.style.backgroundImage = "url('https://source.unsplash.com/1600x900/?city " + name + "')"; document.body.style.backgroundRepeat = "none"; document.body.style.backgroundSize = "100"; document.body.style.width = "100%"; document.body.style.height = "100%"; document.body.style.backgroundRepeat = "no-repeat"; document.body.style.backgroundSize = "cover"; }, search: function () { this.fetchWeather(document.querySelector(".search-bar").value); }, }; document.querySelector(".search button").addEventListener("click", function () { weather.search(); }); document .querySelector(".search-bar") .addEventListener("keyup", function (event) { if (event.key == "Enter") { weather.search(); } }); weather.fetchWeather("Shanghai");
現在,當您完成編碼后,您可以在您的網站上托管您自己的天氣應用程序,或者您甚至可以在 Github 上免費托管它!!!
https://github.com/wanghao221/Weather.io
托管是可選的,但我建議將其發布并與您的朋友和家人共享,并將其添加到您的項目列表中。
即將推出的功能
這是我計劃添加一些更酷的功能,例如
每當您打開網站時進行位置檢測,它將顯示其天氣特定位置的相關天氣新聞使背景圖像更準確地顯示位置使其對大多數設備(iPad 和平板電腦)的響應速度更快
項目中一些很酷的截圖
以上是“怎么用JS和API制作天氣Web應用程序”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創新互聯行業資訊頻道!