-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
188 lines (162 loc) · 4.63 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Floweather - Weather Dashboard</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #e0f7fa;
margin: 0;
padding: 0;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.container {
background-color: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.2);
max-width: 400px;
width: 100%;
text-align: center;
}
h1 {
color: #00796b;
font-size: 2em;
margin-bottom: 20px;
}
.search-box {
display: flex;
margin-bottom: 20px;
}
#city-input {
flex: 1;
padding: 10px;
font-size: 16px;
border: 1px solid #00796b;
border-radius: 5px;
outline: none;
}
#search-btn {
padding: 10px 20px;
margin-left: 10px;
font-size: 16px;
background-color: #00796b;
color: white;
border: none;
cursor: pointer;
border-radius: 5px;
transition: background-color 0.3s ease;
}
#search-btn:hover {
background-color: #004d40;
}
#weather-info {
display: none;
margin-top: 20px;
}
#city-name {
font-size: 1.5em;
color: #00796b;
}
.weather-main {
display: flex;
align-items: center;
justify-content: center;
}
#weather-icon {
width: 80px;
margin-right: 20px;
}
#temperature {
font-size: 2em;
font-weight: bold;
color: #00796b;
}
#weather-description {
text-transform: capitalize;
}
#humidity, #wind-speed {
margin-top: 10px;
font-size: 1em;
color: #00796b;
}
footer {
margin-top: 30px;
font-size: 0.9em;
color: #757575;
}
</style>
</head>
<body>
<div class="container">
<h1>Floweather</h1>
<div class="search-box">
<input type="text" id="city-input" placeholder="Enter city name" />
<button id="search-btn">Search</button>
</div>
<div id="weather-info">
<h2 id="city-name"></h2>
<div class="weather-main">
<img id="weather-icon" alt="Weather Icon">
<p id="temperature"></p>
</div>
<p id="weather-description"></p>
<p id="humidity"></p>
<p id="wind-speed"></p>
</div>
<footer>Powered by Floweather</footer>
</div>
<script>
const apiKey = 'b6907d289e10d714a6e88b30761fae22'; // Public demo API key from OpenWeatherMap
const weatherInfo = document.getElementById('weather-info');
const cityName = document.getElementById('city-name');
const weatherIcon = document.getElementById('weather-icon');
const temperature = document.getElementById('temperature');
const weatherDescription = document.getElementById('weather-description');
const humidity = document.getElementById('humidity');
const windSpeed = document.getElementById('wind-speed');
const searchBtn = document.getElementById('search-btn');
const cityInput = document.getElementById('city-input');
// Fetch weather data from OpenWeatherMap API
const getWeatherData = async (city) => {
const url = `https://openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
try {
const response = await fetch(url);
const data = await response.json();
if (data.cod === "404") {
alert("City not found! Please try again.");
} else {
displayWeather(data);
}
} catch (error) {
console.error("Error fetching weather data:", error);
alert("Something went wrong. Please try again later.");
}
};
// Display weather data in the UI
const displayWeather = (data) => {
cityName.textContent = `${data.name}, ${data.sys.country}`;
temperature.textContent = `${data.main.temp}°C`;
weatherDescription.textContent = `${data.weather[0].description}`;
humidity.textContent = `Humidity: ${data.main.humidity}%`;
windSpeed.textContent = `Wind Speed: ${data.wind.speed} m/s`;
weatherIcon.src = `http://openweathermap.org/img/wn/${data.weather[0].icon}@2x.png`;
weatherInfo.style.display = "block";
};
// Event listener for search button
searchBtn.addEventListener('click', () => {
const city = cityInput.value.trim();
if (city) {
getWeatherData(city);
} else {
alert("Please enter a city name.");
}
});
</script>
</body>
</html>