blob: a068560e51b12d78a43609cb3b62988ab53da4b6 [file] [log] [blame]
Davit Tabidze71eecce2024-06-25 15:00:55 +04001let mouseOverEnabled = true;
2
3function handleImageChange(imageURL, index, factTitle) {
4 const factImage = document.getElementById("factImage");
5 if (factImage.src.endsWith(imageURL)) {
6 return;
7 };
8 highlightFact(index);
9 factImage.classList.add("fade-out");
10 setTimeout(() => {
11 factImage.src = imageURL;
12 factImage.alt = factTitle;
13 factImage.classList.remove("fade-out");
14 }, 300);
15};
16
17function highlightFact(index) {
18 const facts = document.querySelectorAll(".facts");
19 facts.forEach((fact, i) => {
20 if (i === index) {
21 fact.classList.add("active-fact");
22 } else {
23 fact.classList.remove("active-fact");
24 };
25 });
26};
27
28function handleMouseover(imageURL, index, factTitle) {
29 if (!mouseOverEnabled) {
30 return;
31 };
32 handleImageChange(imageURL, index, factTitle);
33};
34
35let scrollTimeout;
36let mouseoverTimeout;
37
38function delayScroll(func, delay) {
39 return function (...args) {
40 clearTimeout(scrollTimeout);
41 scrollTimeout = setTimeout(() => {
42 func.apply(this, args);
43 }, delay);
44 };
45}
46
47function delayMouseoverEnable(delay) {
48 clearTimeout(mouseoverTimeout);
49 mouseoverTimeout = setTimeout(() => {
50 mouseOverEnabled = true;
51 }, delay);
52}
53
54let lastScrollTop = 0;
55
56function getFirstVisibleFact() {
57 const facts = document.querySelectorAll(".facts");
58 const factImage = document.getElementById("factImageBox");
59 const imageBottom = factImage.getBoundingClientRect().bottom;
60
61 const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
62 const isScrollingDown = scrollTop > lastScrollTop;
63 lastScrollTop = scrollTop;
64
65 if (isScrollingDown) {
66 for (let i = 0; i < facts.length; i++) {
67 const rect = facts[i].getBoundingClientRect();
68 if (rect.top < window.innerHeight && rect.top >= imageBottom) {
69 return i;
70 };
71 };
72 return 7;
73 } else {
74 for (let i = 0; i < facts.length; i++) {
75 const rect = facts[i].getBoundingClientRect();
76 if (rect.top >= 0 && rect.top >= imageBottom) {
77 return i;
78 };
79 };
80 };
81 return 0;
82};
83
84document.addEventListener("DOMContentLoaded", function () {
85 const gridContainer = document.querySelector(".grid-container");
86 const facts = JSON.parse(gridContainer.dataset.facts);
87
88 const handleScroll = () => {
89 clearTimeout(scrollTimeout);
90 mouseOverEnabled = false;
91 scrollTimeout = setTimeout(() => {
92 const currentSection = getFirstVisibleFact();
93 handleImageChange(facts[currentSection].params.image, currentSection, facts[currentSection].params.title);
94 delayMouseoverEnable(800);
95 }, 300);
96 };
97
98 const checkAddRemoveScrollListener = () => {
99 if (window.innerWidth <= 768) {
100 window.addEventListener("scroll", handleScroll);
101 } else {
102 window.removeEventListener("scroll", handleScroll);
103 }
104 };
105
106 checkAddRemoveScrollListener();
107
Davit Tabidzef2aa7502024-07-25 18:02:03 +0400108 let resizing = false;
109 const resizeDelayTime = 1000;
110 let resizeTimeout;
111
112 const startResizing = () => {
113 clearTimeout(resizeTimeout);
114 resizing = true;
115 document.querySelectorAll(".facts").forEach(fact => {
116 fact.removeEventListener("mouseover", handleMouseover);
117 });
118 };
119
120 const stopResizing = () => {
121 clearTimeout(resizeTimeout);
122 resizeTimeout = setTimeout(() => {
123 resizing = false;
124 checkAddRemoveScrollListener();
125 document.querySelectorAll(".facts").forEach((fact, index) => {
126 fact.addEventListener("mouseover", () => handleMouseover(facts[index].params.image, index, facts[index].params.title));
127 });
128 }, resizeDelayTime);
129 };
130
131 window.addEventListener("resize", () => {
132 if (!resizing) {
133 startResizing();
134 }
135 stopResizing();
136 });
137
138 document.querySelectorAll(".facts").forEach((fact, index) => {
139 fact.addEventListener("mouseover", () => handleMouseover(facts[index].params.image, index, facts[index].params.title));
140 });
Davit Tabidze71eecce2024-06-25 15:00:55 +0400141});