-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclimate_change.js
170 lines (115 loc) · 4.04 KB
/
climate_change.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'use strict';
/**
* Add event listener on multiple elements
*/
const addEventOnElements = function (elements, eventType, callback) {
for (let i = 0, len = elements.length; i < len; i++) {
elements[i].addEventListener(eventType, callback);
}
}
/**
* MOBILE NAVBAR TOGGLER
*/
const navbar = document.querySelector("[data-navbar]");
const navTogglers = document.querySelectorAll("[data-nav-toggler]");
const toggleNav = () => {
navbar.classList.toggle("active");
document.body.classList.toggle("nav-active");
}
addEventOnElements(navTogglers, "click", toggleNav);
/**
* HEADER ANIMATION
* When scrolled donw to 100px header will be active
*/
const header = document.querySelector("[data-header]");
const backTopBtn = document.querySelector("[data-back-top-btn]");
window.addEventListener("scroll", () => {
if (window.scrollY > 100) {
header.classList.add("active");
backTopBtn.classList.add("active");
} else {
header.classList.remove("active");
backTopBtn.classList.remove("active");
}
});
/**
* SLIDER
*/
const slider = document.querySelector("[data-slider]");
const sliderContainer = document.querySelector("[data-slider-container]");
const sliderPrevBtn = document.querySelector("[data-slider-prev]");
const sliderNextBtn = document.querySelector("[data-slider-next]");
let totalSliderVisibleItems = Number(getComputedStyle(slider).getPropertyValue("--slider-items"));
let totalSlidableItems = sliderContainer.childElementCount - totalSliderVisibleItems;
let currentSlidePos = 0;
const moveSliderItem = function () {
sliderContainer.style.transform = `translateX(-${sliderContainer.children[currentSlidePos].offsetLeft}px)`;
}
/**
* NEXT SLIDE
*/
const slideNext = function () {
const slideEnd = currentSlidePos >= totalSlidableItems;
if (slideEnd) {
currentSlidePos = 0;
} else {
currentSlidePos++;
}
moveSliderItem();
}
sliderNextBtn.addEventListener("click", slideNext);
/**
* PREVIOUS SLIDE
*/
const slidePrev = function () {
if (currentSlidePos <= 0) {
currentSlidePos = totalSlidableItems;
} else {
currentSlidePos--;
}
moveSliderItem();
}
sliderPrevBtn.addEventListener("click", slidePrev);
/**
* RESPONSIVE
*/
window.addEventListener("resize", function () {
totalSliderVisibleItems = Number(getComputedStyle(slider).getPropertyValue("--slider-items"));
totalSlidableItems = sliderContainer.childElementCount - totalSliderVisibleItems;
moveSliderItem();
});
"use strict";
// Function to fetch weather data for the past 7 days
const fetchWeatherData = async () => {
const apiKey = "4c21304b59da33a8c0d0f41e25355d81"; // Replace with your actual API key
const apiUrl = `https://api.weatherapi.com/v1/history.json?key=${apiKey}&q=LOCATION&dt=DATE`;
try {
const response = await fetch(apiUrl);
const data = await response.json();
return data;
} catch (error) {
console.error("Error fetching weather data:", error);
return null;
}
};
// Function to update temperature monitor
const updateTemperatureMonitor = async () => {
const days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
const currentDate = new Date();
const temperatureMonitor = document.getElementById("temperature-monitor");
for (let i = 6; i >= 0; i--) {
const date = new Date(currentDate);
date.setDate(date.getDate() - i);
const formattedDate = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, "0")}-${date.getDate().toString().padStart(2, "0")}`;
// Fetch weather data for the date
const weatherData = await fetchWeatherData(formattedDate);
const temperature = weatherData?.forecast?.forecastday[0]?.day?.avgtemp_c;
// Update HTML with temperature information
const dayElement = temperatureMonitor.querySelector(`[data-day="${days[date.getDay()]}"]`);
if (dayElement) {
dayElement.textContent = `${days[date.getDay()]}: ${temperature}°C`;
}
}
};
// Call updateTemperatureMonitor function when the page loads
window.addEventListener("load", updateTemperatureMonitor);