forked from sergiokopplin/indigo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgallery_scripts.js
More file actions
49 lines (43 loc) · 1.58 KB
/
gallery_scripts.js
File metadata and controls
49 lines (43 loc) · 1.58 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
document.addEventListener("DOMContentLoaded", function() {
const gallery = document.querySelector(".gallery");
const items = Array.from(gallery.children);
const sortOrderSelect = document.getElementById('sort-order');
// Shuffle gallery items randomly
function shuffleGallery() {
items.sort(() => Math.random() - 0.5);
renderGallery();
}
// Sort gallery items by likes
function sortByLikes() {
items.sort((a, b) => {
return parseInt(b.getAttribute('data-likes')) - parseInt(a.getAttribute('data-likes'));
});
renderGallery();
}
// Render gallery items
function renderGallery() {
gallery.innerHTML = "";
items.forEach(item => gallery.appendChild(item));
}
// Listen for changes in sort order
sortOrderSelect.addEventListener('change', function() {
if (this.value === 'random') {
shuffleGallery();
} else if (this.value === 'likes') {
sortByLikes();
}
});
// Initial shuffle display
shuffleGallery();
// Like button functionality
gallery.addEventListener('click', function(e) {
if (e.target.classList.contains('like-btn')) {
const galleryItem = e.target.closest('.gallery-item');
const likeCountSpan = galleryItem.querySelector('.like-count');
let currentLikes = parseInt(galleryItem.getAttribute('data-likes'));
currentLikes++;
galleryItem.setAttribute('data-likes', currentLikes);
likeCountSpan.textContent = currentLikes;
}
});
});