├── README.md ├── app.js ├── index.html └── style.css /README.md: -------------------------------------------------------------------------------- 1 | # Weather-App-JS 2 | A weather app developed using javascript with weather API 3 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | let weather = { 2 | apikey: "5bc2b4f40acfffa046713955a4370d52", 3 | fetchWeather: function (city) { 4 | fetch( 5 | "https://api.openweathermap.org/data/2.5/weather?q=" + 6 | city + 7 | "&units=metric&appid=" + 8 | this.apikey 9 | ) 10 | .then((response) => { 11 | return response.json(); 12 | }) 13 | .then((data) => this.displayWeather(data)); 14 | }, 15 | displayWeather: function (data) { 16 | if (data.cod == "200") { 17 | getweather(data); 18 | } 19 | else if (data.cod == "400") { 20 | noinput(data); 21 | } 22 | else{ 23 | citynotfound(data); 24 | } 25 | }, 26 | search: function () { 27 | this.fetchWeather(document.querySelector(".search-bar").value); 28 | }, 29 | }; 30 | 31 | document.querySelector(".search button").addEventListener("click", function () { 32 | weather.search(); 33 | }); 34 | document 35 | .querySelector(".search-bar") 36 | .addEventListener("keyup", function (event) { 37 | if (event.key == "Enter") weather.search(); 38 | }); 39 | weather.fetchWeather("Greater Noida"); 40 | 41 | 42 | function getweather(data){ 43 | const { name } = data; 44 | const { description, icon } = data.weather[0]; 45 | const { temp, humidity } = data.main; 46 | const { speed } = data.wind; 47 | // console.log(name, icon, description, temp, humidity, speed); 48 | document.querySelector(".location-city").innerHTML = "Weather in " + name; 49 | document.querySelector(".icon").style.backgroundImage = "url(https://openweathermap.org/img/wn/" + icon + "@2x.png)"; 50 | document.querySelector(".temperature-degree").innerHTML = temp + "°C"; 51 | document.querySelector(".weather-description").innerHTML = description; 52 | document.querySelector(".humidity").innerHTML = "Humidity: " + humidity + "%"; 53 | document.querySelector(".wind").innerHTML = "Wind: " + speed + "Km/hr"; 54 | document.querySelector(".weather ").classList.remove("loading"); 55 | document.body.style.backgroundImage = "url('https://source.unsplash.com/1600x900/?" + description + "')"; 56 | document.querySelector(".temperature-degree").style.position = "relative"; 57 | document.querySelector(".temperature-degree").style.visibility = "visible"; 58 | document.querySelector(".humidity").style.position = "relative"; 59 | document.querySelector(".humidity").style.visibility = "visible"; 60 | document.querySelector(".wind").style.position = "relative"; 61 | document.querySelector(".wind").style.visibility = "visible"; 62 | } 63 | 64 | function noinput(data){ 65 | document.querySelector(".location-city").innerHTML = "Something went wrong!"; 66 | document.querySelector(".temperature-degree").style.position = "absolute"; 67 | document.querySelector(".temperature-degree").style.visibility = "hidden"; 68 | document.querySelector(".humidity").style.position = "absolute"; 69 | document.querySelector(".humidity").style.visibility = "hidden"; 70 | document.querySelector(".wind").style.position = "absolute"; 71 | document.querySelector(".wind").style.visibility = "hidden"; 72 | } 73 | 74 | function citynotfound(data){ 75 | document.querySelector(".location-city").innerHTML = "City not found"; 76 | document.querySelector(".temperature-degree").style.position = "absolute"; 77 | document.querySelector(".temperature-degree").style.visibility = "hidden"; 78 | document.querySelector(".humidity").style.position = "absolute"; 79 | document.querySelector(".humidity").style.visibility = "hidden"; 80 | document.querySelector(".wind").style.position = "absolute"; 81 | document.querySelector(".wind").style.visibility = "hidden"; 82 | } 83 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 |