-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
143 lines (111 loc) · 3.83 KB
/
script.js
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
let loc = document.getElementById("d1");
let tempicon1 = document.getElementById("image_00");
let tempvalue1 = document.getElementById("temp_00");
let climate1 = document.getElementById("climate_00");
let iconfile;
let ap=document.getElementById("d12");
let hum=document.getElementById("humidity_00");
let wind1=document.getElementById("wind_00");
const searchInput = document.getElementById("input");
const searchButton = document.getElementById("search_button");
var d = new Date();
var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",];
searchButton.addEventListener('click', (e) => {
e.preventDefault();
getWeather(searchInput.value);
searchInput.value = '';
})
const getWeather = async (city) => {
try {
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=53a46d7e7f146a89a6425ac9ebc136b3`,
{ mode: 'cors' }
);
const weatherData = await response.json();
console.log(weatherData);
const { name } = weatherData;
const { feels_like, humidity } = weatherData.main;
const { id, main } = weatherData.weather[0];
const { speed} = weatherData.wind;
loc.textContent = name;
ap.textContent =name;
climate1.textContent = main;
tempvalue1.textContent = Math.round(feels_like - 273);
hum.textContent=humidity;
wind1.textContent=speed;
tempicon1.src = "http://openweathermap.org/img/wn/"+
weatherData.weather[0].icon
+".png";
}
catch (error)
{
alert('city not found');
}
};
var nav =document.querySelector('#NaVbar');
window.addEventListener('scroll',function(){
if (window.pageYOffset>30) {
nav.classList.add('fixed-top');
}else{
nav.classList.remove('fixed-top');
}
});
let myNav = document.querySelector('#NaVbar');
window.onscroll = function () { moveTop() };
function GetInfo() {
var newName = document.getElementById("input");
fetch('https://api.openweathermap.org/data/2.5/forecast?q='+newName.value+'&appid=53a46d7e7f146a89a6425ac9ebc136b3')
.then(response => response.json())
.then(data => {
let n;
let date=new Date();
let mon=date.getHours();
console.log(mon);
if(mon>=0 & mon<3){
n=1;
} else if(mon>=3 & mon<6){
n=2;
} else if(mon>=6 & mon<9){
n=3;
} else if(mon>=9& mon<12){
n=4;
} else if(mon>=12 & mon<15){
n=5;
} else if(mon>=15 & mon<18){
n=6;
} else if(mon>=18 & mon<21){
n=7;
}else {
n=7;
}
let m=8-n;
let q=m+2;
for (let d = 1; d <5; d++) {
document.getElementById("d0" + (d+1)).innerHTML =data.list[q].dt_txt.slice(0,10);
document.getElementById("d" + (d+1)).innerHTML =data.list[q].dt_txt.slice(0,10);
q=q+8;
}
for (let a = 0; a < 4; a++) {
for (let b = 1; b < 9; b++) {
document.getElementById("temp_" + (a)+ +(b)).innerHTML =Number(data.list[m+2].main.feels_like - 273.15).toFixed(1);
document.getElementById("climate_" +(a)+ +(b)).innerHTML =data.list[m+2].weather[0].main;
document.getElementById("image_" +(a)+ +(b)).src = "http://openweathermap.org/img/wn/"+
data.list[m+2].weather[0].icon
+".png";
m++;
}
}
console.log(data)
function CheckDay(day){
if(day + d.getDay() > 6){
return day + d.getDay() - 7;
}
else{
return day + d.getDay();
}
}
for(i = 1; i<5; i++){
document.getElementById("a" + (i+1)).innerHTML = weekday[CheckDay(i)];
}
})
.catch(err => alert("City not found"))
}