-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
135 lines (127 loc) · 4.98 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Weather</title>
<link rel="stylesheet" href="styles.css" />
<link rel="website icon" type="png" href="Images/haze.png" />
</head>
<body>
<div id="loading-screen">
<div class="card2">
<div class="screen">
<img src="Images/clouds.png" width="300" height="300px">
</div>
<h1 class="text">WEATHER</h1>
<p>-By CodeCrafters</p>
</div>
</div>
<div class="card">
<div class="search">
<input type="text" placeholder="Enter the city" spellcheck="false" />
<button>
<img src="Images/search.png" />
</button>
</div>
<div class="error">
<p>Invalid City name</p>
</div>
<div class="weather">
<img src="Images/clear.png" class="Weather-icon" />
<h1 class="temp">30°C</h1>
<p class="weather-condition">Clear</p>
<h2 class="city">Nashik</h2>
<p class="underline">___________</p>
<div class="Extra-info">
<div class="col">
<img src="Images/humidity.png" id="humidity-img"/>
<div>
<p class="humidity">52 %</p>
<p class="col-p">Humidity</p>
</div>
</div>
<div class="col">
<img src="Images/wind.png" id="wind-img"/>
<div>
<p class="wind">8.2 km/h</p>
<p class="col-p">wind-speed</p>
</div>
</div>
</div>
</div>
</div>
<script>
document.addEventListener("DOMContentLoaded",function(event){
setTimeout(function(){
var loadingScreen = document.getElementById("loading-screen");
loadingScreen.style.display = "none";
var content = document.getElementsByClassName("card")[0];
content.style.display = "block";
}, 2500);
});
const apiKey = "7cd8daa52e2045fdf442706f60e0c21b";
const apiUrl =
"https://api.openweathermap.org/data/2.5/weather?units=metric&q=";
const searchBox = document.querySelector(".search input");
const searchBtn = document.querySelector(".search button");
const weatherIcon = document.querySelector(".Weather-icon");
const weatherColor = document.querySelector(".card");
async function CheckWeather(city) {
const response = await fetch(apiUrl + city + `&appid=${apiKey}`);
if (response.status == 404) {
document.querySelector(".error").style.display = "block";
document.querySelector(".weather").style.display = "none";
} else {
var data = await response.json();
document.querySelector(".city").innerHTML = data.name;
document.querySelector(".temp").innerHTML =
Math.round(data.main.temp) + "°C";
document.querySelector(".weather-condition").innerHTML =
data.weather[0].main;
document.querySelector(".humidity").innerHTML =
data.main.humidity + "%";
document.querySelector(".wind").innerHTML = data.wind.speed + "km/h";
if (data.weather[0].main == "Clouds") {
weatherIcon.src = "Images/clouds.png";
weatherColor.style.backgroundColor = "#7fbae5";
} else if (data.weather[0].main == "Clear") {
weatherIcon.src = "Images/clear.png";
weatherColor.style.backgroundColor = "#6ec2ff";
} else if (data.weather[0].main == "Haze") {
weatherIcon.src = "Images/haze.png";
weatherColor.style.backgroundColor = "#b7c6d2";
} else if (data.weather[0].main == "Rain") {
weatherIcon.src = "Images/rain.png";
weatherColor.style.backgroundColor = "#375367";
} else if (data.weather[0].main == "Drizzle") {
weatherIcon.src = "Images/drizzle.png";
weatherColor.style.backgroundColor = "#5982a1";
} else if (data.weather[0].main == "Snow") {
weatherIcon.src = "Images/snow.png";
weatherColor.style.backgroundColor = "#bbd1e1";
} else if (data.weather[0].main == "Lightning") {
weatherIcon.src = "Images/lightning.png";
weatherColor.style.backgroundColor = "#576168";
}
document.querySelector(".weather").style.display = "block";
document.querySelector(".error").style.display = "none";
}
}
searchBtn.addEventListener("click", () => {
CheckWeather(searchBox.value);
});
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('/service-worker.js')
.then(function(registration) {
console.log('Service Worker registered with scope:', registration.scope);
})
.catch(function(error) {
console.error('Service Worker registration failed:', error);
});
});
}
</script>
</body>
</html>