-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.js
More file actions
261 lines (213 loc) · 6.76 KB
/
logic.js
File metadata and controls
261 lines (213 loc) · 6.76 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
"use strict";
// ----- scroll-to-top button -----
// Select the scroll-to-top button
const scrollToTopBtn = document.querySelector(".scroll_up");
// Select the trigger element for the sticky button
const scrollTrigger = document.querySelector(".slideshow_container");
// Function to toggle the sticky button based on intersection
const toggleStickyButton = (entries) => {
const [entry] = entries;
if (!entry.isIntersecting) {
// Add sticky class when the trigger element is not intersecting
scrollToTopBtn.classList.add("sticky");
} else {
// Remove sticky class when the trigger element is intersecting
scrollToTopBtn.classList.remove("sticky");
}
};
// Get the height of the trigger element
const containerHeight = scrollTrigger.getBoundingClientRect().height;
// Create an IntersectionObserver to observe the trigger element
const breakpointObserver = new IntersectionObserver(toggleStickyButton, {
root: null,
threshold: 0,
rootMargin: `-${containerHeight}px`,
});
// Start observing the trigger element
breakpointObserver.observe(scrollTrigger);
// Function to scroll to the top of the page
const scrollToTop = () => {
window.scrollTo({
top: 0,
behavior: "smooth",
});
};
// Add click event listener to the scroll-to-top button
scrollToTopBtn.addEventListener("click", scrollToTop);
// ----- slider Img -----
// Preload images to improve performance
const preloadImages = (images) => {
for (let i = 0; i < images.length; i++) {
const img = new Image();
img.src = images[i];
}
};
let sliderImgs = [];
let currentImageIndex = 0;
let lastImg = false;
const newImg = new Image();
// Function to increment the current image index
const incrementImageIndex = () => {
currentImageIndex++;
if (currentImageIndex >= sliderImgs.length) {
currentImageIndex = 0;
}
sliderImgEl();
};
// Function to decrement the current image index
const decrementImageIndex = () => {
currentImageIndex--;
if (currentImageIndex < 0) {
currentImageIndex = sliderImgs.length - 1;
}
sliderImgEl();
};
// Function to render the slider image element
const sliderImgEl = () => {
const slideshowContainer = document.querySelector(".slideshow_container");
const html = `
<figure class="img_container">
<img src="${sliderImgs[currentImageIndex]}" alt="Matcha Bild" />
</figure>
<i class="fa-solid fa-angle-left left"></i>
<i class="fa-solid fa-angle-right right"></i>
`;
slideshowContainer.innerHTML = html;
const incrementBtn = document.querySelector(".right");
const decrementBtn = document.querySelector(".left");
incrementBtn.addEventListener("click", incrementImageIndex);
decrementBtn.addEventListener("click", decrementImageIndex);
newImg.src = sliderImgs[currentImageIndex];
newImg.addEventListener("load", () => {
if (lastImg) lastImg.remove();
lastImg = newImg;
});
};
// Function to render the slider images
const renderSliderImg = (data) => {
sliderImgs = data;
sliderImgEl();
setInterval(incrementImageIndex, 4000);
preloadImages(sliderImgs);
};
// ----- slider title -----
let currentTitleIndex = 0;
let lastTitle = false;
let sliderTitle = [];
// Function to increment the current title index
const incrementTitle = () => {
currentTitleIndex++;
if (currentTitleIndex >= sliderTitle.length) {
currentTitleIndex = 0;
}
sliderTitleEl();
};
// Function to render the slider title element
const sliderTitleEl = () => {
const currentTitleElement = document.querySelector(".title_container");
currentTitleElement.innerHTML = sliderTitle[currentTitleIndex];
};
// Function to render the slider titles
const renderSliderTitle = (data) => {
sliderTitle = data;
sliderTitleEl();
setInterval(incrementTitle, 4000);
};
// ----- about / modal -----
const aboutSection = document.querySelector(".about_container");
const modalSection = document.querySelector(".modal_container");
// Function to open the modal
const showModal = (index) => {
renderModalData(modalData, index);
modalSection.style.display = "block";
};
// Function to close the modal
const closeModal = () => {
modalSection.style.display = "none";
};
// Event listener to close the modal when the close icon is clicked
document.addEventListener("click", (event) => {
if (event.target.classList.contains("close")) {
closeModal();
}
});
// Function to render the modal data
const renderModalData = (data, index) => {
const html = `
<div class="modal_content">
<i class="fa-regular fa-circle-xmark close"></i>
<h3>${data[index].title}</h3>
<p>${data[index].text}</p>
</div>
`;
modalSection.innerHTML = html;
};
// Function to render the about section data
const renderAboutData = (data) => {
const html = data
.map((item, index) => {
return `
<figure class="about_item" data-index="${index}">
<img src="${item.img}" alt="${item.alt}" />
<figcaption>${item.title}</figcaption>
</figure>
`;
})
.join("");
aboutSection.innerHTML = html;
const aboutItems = document.querySelectorAll(".about_item");
aboutItems.forEach((item) =>
item.addEventListener("click", () => showModal(item.dataset.index))
);
};
// Function to render the gallery section
const renderGallery = (data) => {
const galleryContainer = document.querySelector(".gallery_container");
const html = `
<figure class="gallery_img">
${data
.map(
(imageUrl) => `
<div class="image_container">
<img src="${imageUrl}" alt="Matcha Galerie" />
<figcaption class="icon">
<a href="${imageUrl}" download><i class="fa-solid fa-cloud-arrow-down download"></i></a>
</figcaption>
</div>
`
)
.join("")}
</figure>`;
galleryContainer.innerHTML = html;
};
// Function to fetch JSON data from a URL
async function getJSONData(url) {
try {
const response = await fetch(url);
const data = await response.json();
return data;
} catch (error) {
console.log("Fehler beim Abrufen der JSON-Daten:", error);
alert(`Something went wrong! ${error.message}`);
throw error;
}
}
let modalData;
// Function to fetch and render all the data
async function dataCall() {
try {
const sliderImgData = await getJSONData("sliderImg.json");
renderSliderImg(sliderImgData);
const sliderTitleData = await getJSONData("sliderTitle.json");
renderSliderTitle(sliderTitleData);
const aboutData = await getJSONData("about.json");
renderAboutData(aboutData);
modalData = await getJSONData("modal.json");
const galleryData = await getJSONData("gallery.json");
renderGallery(galleryData);
} catch (error) {
console.log("Fehler beim Abrufen der JSON-Daten:", error);
}
}
// Call the dataCall function to fetch and render the data
dataCall();