-
Notifications
You must be signed in to change notification settings - Fork 0
/
load-more.js
37 lines (32 loc) · 1.34 KB
/
load-more.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
document.addEventListener("DOMContentLoaded", () => {
// Function to hide gallery items beyond the specified limit
const setupGalleries = () => {
const isTabletOrAbove = window.matchMedia("(min-width: 992px)").matches;
const initialVisibleItems = isTabletOrAbove ? 8 : 4;
document.querySelectorAll('.gallery_component').forEach(gallery => {
const items = Array.from(gallery.children);
items.forEach((item, index) => {
if (index >= initialVisibleItems) item.classList.add('hide');
});
});
};
// Function to handle the "load more" functionality
const setupLoadMoreButtons = () => {
document.querySelectorAll('.gallery_load-more').forEach(button => {
button.addEventListener('click', function() {
const galleryId = this.getAttribute('data-target');
const gallery = document.getElementById(galleryId);
const hiddenItems = gallery.querySelectorAll('.hide');
// Show the next set of hidden items (up to four)
Array.from(hiddenItems).slice(0, 4).forEach(item => item.classList.remove('hide'));
// Hide the "load more" button if there are no more hidden items
if (gallery.querySelectorAll('.hide').length === 0) {
this.style.display = 'none';
}
});
});
};
// Initial setup
setupGalleries();
setupLoadMoreButtons();
});