-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
78 lines (71 loc) · 2.31 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
let img=document.querySelector("#img")
let body=document.querySelector(".input").addEventListener("keydown",function(e){
if(e.key=="Enter"){
main();
}
})
img.addEventListener("click",main);
async function main(){
weather=document.querySelector(".weather");
container1=document.querySelector(".container1");
if(weather.classList.contains("open")==false){
weather.style.transitionDelay="0.4s"
await finddweather()
weather.classList.toggle("open");
container1.classList.toggle("trans");
}
else if(weather.classList.contains("open")) {
weather.style.transitionDelay="0s"
weather.classList.toggle("open");
container1.classList.toggle("trans");
finddweather();
setTimeout(function(){
weather.classList.toggle("open");
container1.classList.toggle("trans");
},600);
}
}
// No city found alert
function customalert(){
document.querySelector(".container3").classList.toggle("show");
document.querySelector(".layover").classList.toggle("show");
}
// Alter close function
let close=document.querySelector(".close");
close.addEventListener("click",function(){
customalert();
})
async function finddweather(){
var apikey="edd892fa5ac640ffb5e101714231311"
var url="https://api.weatherapi.com/v1/current.json?"
var q=document.querySelector(".input").value
await fetch(`${url}key=${apikey}&q=${q}`)
.then(data=>data.json())
.then((data)=>{
findandshow(data);
}
)
}
function findandshow(data){
console.log(data.error)
if(data.error){
customalert();
}
else{
console.log(data);
document.querySelector(".mainweatherimg").src=data.current.condition.icon;
document.querySelector(".comment").innerHTML=data.current.condition.text;
document.querySelector(".temp").innerHTML=Math.round(data.current.temp_c) + " °C";
document.querySelector(".humval").innerHTML=data.current.humidity+ " %";
document.querySelector(".winval").innerHTML=data.current.wind_kph+ " Km/hr";
}
}
let input=document.querySelector(".input");
input.addEventListener("input",function(){
let inval=input.value;
fir=inval.charAt(0);
fir=fir.toUpperCase();
rest=inval.slice(1);
rest=rest.toLowerCase();
input.value=fir+rest;
})