-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
204 lines (168 loc) · 4.66 KB
/
Copy pathindex.html
File metadata and controls
204 lines (168 loc) · 4.66 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Weather App</title>
<style>
body {
margin: 0;
font-family: Arial, sans-serif;
background: linear-gradient(135deg, #396184, #97bdbe);
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
/* App container */
.app {
background: rgba(255, 255, 255, 0.15);
padding: 30px;
border-radius: 20px;
backdrop-filter: blur(10px);
width: 320px;
text-align: center;
color: white;
}
/* Search */
.search {
display: flex;
gap: 5px;
}
input {
flex: 1;
padding: 10px;
border-radius: 10px;
border: none;
}
button {
padding: 10px;
border: none;
border-radius: 10px;
cursor: pointer;
}
/* Weather card */
.weather-card {
margin-top: 20px;
}
.weather-card img {
width: 80px;
}
.extra {
display: flex;
justify-content: space-between;
margin-top: 20px;
}
.hidden {
display: none;
}
.error {
color: #ffcccc;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="app">
<h1>Weather App</h1>
<div class="search">
<input type="text" id="cityInput" placeholder="Enter city">
<button onclick="getWeather()">Search</button>
<button onclick="getLocationWeather()">📍</button>
</div>
<div id="loader" class="hidden">Loading...</div>
<div id="weatherCard" class="weather-card hidden">
<h2 id="cityName"></h2>
<img id="icon">
<h1 id="temp"></h1>
<p id="desc"></p>
<div class="extra">
<div>
<p>Humidity</p>
<h3 id="humidity"></h3>
</div>
<div>
<p>Wind</p>
<h3 id="wind"></h3>
</div>
<div>
<p>Feels</p>
<h3 id="feels"></h3>
</div>
</div>
</div>
<p id="error" class="error"></p>
</div>
<script>
const apiKey = "0e9f8f81332f993e6dec5a77b6b15449";
const loader = document.getElementById("loader");
const card = document.getElementById("weatherCard");
const errorText = document.getElementById("error");
function showLoader() {
loader.classList.remove("hidden");
card.classList.add("hidden");
errorText.innerText = "";
}
function hideLoader() {
loader.classList.add("hidden");
}
function displayWeather(data) {
document.getElementById("cityName").innerText = data.name;
document.getElementById("temp").innerText = data.main.temp + "°C";
document.getElementById("desc").innerText = data.weather[0].description;
document.getElementById("humidity").innerText = data.main.humidity + "%";
document.getElementById("wind").innerText = data.wind.speed + " m/s";
document.getElementById("feels").innerText = data.main.feels_like + "°C";
const icon = data.weather[0].icon;
document.getElementById("icon").src =
"https://openweathermap.org/img/wn/" + icon + "@2x.png";
card.classList.remove("hidden");
}
function getWeather() {
const city = document.getElementById("cityInput").value.trim();
if (!city) {
errorText.innerText = "Enter a city name";
return;
}
showLoader();
fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${apiKey}`)
.then(res => res.json())
.then(data => {
hideLoader();
if (data.cod == 200) {
displayWeather(data);
} else {
errorText.innerText = data.message;
}
})
.catch(() => {
hideLoader();
errorText.innerText = "Network error";
});
}
function getLocationWeather() {
if (!navigator.geolocation) {
errorText.innerText = "Geolocation not supported";
return;
}
showLoader();
navigator.geolocation.getCurrentPosition(position => {
const lat = position.coords.latitude;
const lon = position.coords.longitude;
fetch(`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&units=metric&appid=${apiKey}`)
.then(res => res.json())
.then(data => {
hideLoader();
if (data.cod == 200) {
displayWeather(data);
} else {
errorText.innerText = data.message;
}
});
}, () => {
hideLoader();
errorText.innerText = "Location permission denied";
});
}
</script>
</body>
</html>