-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather.html
More file actions
96 lines (88 loc) · 2.51 KB
/
weather.html
File metadata and controls
96 lines (88 loc) · 2.51 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Beautiful Weather App</title>
<style>
body {
margin: 0;
padding: 0;
font-family: 'Segoe UI', sans-serif;
background: linear-gradient(to right, #6dd5ed, #2193b0);
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.card {
background: rgba(255, 255, 255, 0.1);
padding: 30px;
border-radius: 20px;
box-shadow: 0 8px 32px 0 rgba(0,0,0,0.2);
backdrop-filter: blur(10px);
color: #fff;
width: 300px;
text-align: center;
}
input {
padding: 10px;
border: none;
border-radius: 10px;
width: 80%;
margin: 10px 0;
font-size: 16px;
}
button {
padding: 10px 20px;
background: #ffffff33;
border: none;
border-radius: 10px;
color: white;
cursor: pointer;
font-weight: bold;
}
.weather img {
width: 80px;
height: 80px;
margin: 10px 0;
}
.weather p {
margin: 5px 0;
font-size: 18px;
}
</style>
</head>
<body>
<div class="card">
<h2>Weather App</h2>
<input type="text" id="cityInput" placeholder="Enter city name" />
<button onclick="getWeather()">Search</button>
<div class="weather" id="weatherResult"></div>
</div>
<script>
const apiKey = "e4ad3cb25d20eac29cfbc53644612f24"; // Replace with your actual key
function getWeather() {
const city = document.getElementById("cityInput").value;
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
fetch(url)
.then(response => response.json())
.then(data => {
if (data.cod === "404") {
document.getElementById("weatherResult").innerHTML = "<p>City not found!</p>";
} else {
document.getElementById("weatherResult").innerHTML = `
<img src="https://openweathermap.org/img/wn/${data.weather[0].icon}@2x.png" alt="Weather Icon" />
<p><strong>${data.name}, ${data.sys.country}</strong></p>
<p>${data.weather[0].main} - ${data.weather[0].description}</p>
<p>Temperature: ${data.main.temp} °C</p>
`;
}
})
.catch(() => {
document.getElementById("weatherResult").innerHTML = "<p>Error fetching weather</p>";
});
}
</script>
</body>
</html>