-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
89 lines (72 loc) · 3.38 KB
/
Copy pathscript.js
File metadata and controls
89 lines (72 loc) · 3.38 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
let weatherContent = document.getElementById("weather");
let details1 = document.getElementById("details-1");
let weather_icon = document.getElementById("image");
let details2 = document.getElementById("details-2");
// let weatherLocation = document.querySelector(".location");
// let weatherDateAndTime = document.querySelector(".date-and-time");
// let currentSituation = document.querySelector(".current-situation");
// let weatherIcon =document.querySelector(".weather-icon");
// let currentTemperature = document.querySelector(".current-temperature");
let feelsLike = document.getElementById("temp");
let humidity = document.getElementById("hum");
let wind = document.getElementById("wi");
let currentPressure = document.getElementById("pre");
async function getWeather() {
let searchCity = document.getElementById("search-place").value;
try {
const weatherApi = `https://api.weatherapi.com/v1/current.json?key=48070a9ad4cd4a92951171804251904&q=${searchCity}&aqi=no`;
const result = await fetch(weatherApi, {
headers: {
Accept: "application/json"
}
})
const data = await result.json();
console.log(data);
feelsLike.innerHTML = `<p>${data.current.feelslike_c}°C</p>`;
humidity.innerText = await data.current.humidity + "%";
wind.innerText = await data.current.wind_kph + "kmph";
currentPressure.innerText = await data.current.pressure_mb + "mb";
// weatherLocation.innerHTML = `<h2>${data.location.name}, ${data.location.country}</h2>`;
// weatherDateAndTime.innerText = await data.location.localtime;
// currentSituation.innerText = await data.current.condition.text;
// weatherIcon.innerHTML = `<img src="${data.current.condition.icon}" alt="icon"></img>`;
// currentTemperature.innerHTML = `<p>${data.current.temp_c}°C</p>`;
details1.innerHTML = `
<h3><p><i class="fa-solid fa-location-dot"></i> </p>${data.location.name}, ${data.location.country}</h3>
<p>${data.location.localtime}</p>
<p id="presentContions">Condition: ${data.current.condition.text}</p>
`;
weather_icon.innerHTML = `<img src="${data.current.condition.icon}" alt="icon">`;
details2.innerHTML = `<p>Temperature: ${data.current.temp_c}°C</p>`;
//changing bg based on day time or night time
let bgVideo = document.getElementById("bg-video");
if(data.current.is_day !=0) {
bgVideo.querySelector("source").src = "/Assets/clear morning sky.mp4";
} else {
bgVideo.querySelector("source").src = "/Assets/clear and night sky.mp4";
}
bgVideo.load();
if(data.current.condition.text === "Sunny") {
bgVideo.querySelector("source").src = "/Assets/sunny.mp4";
}
bgVideo.load();
if(data.current.condition.text === "Rainy") {
bgVideo.querySelector("source").src = "/Assets/sunny.mp4";
}
bgVideo.load();
if(data.current.condition.text === "Partly cloudy") {
bgVideo.querySelector("source").src = "/Assets/partly cloudy.mp4";
}
bgVideo.load();
} catch(error) {
if(error) {
alert('Enter a valid city name!');
}
};
searchCity.value = "";
}
document.getElementById("search-place").addEventListener("keydown", (event) => {
if(event.key === "Enter") {
getWeather();
};
});