-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
66 lines (50 loc) · 1.63 KB
/
main.js
File metadata and controls
66 lines (50 loc) · 1.63 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
const links = document.querySelectorAll('#categories a');
const searchBar = document.getElementById('search-bar');
const items = document.querySelectorAll('.grid .item img');
const overlay = document.getElementById('overlay');
const grid = new Muuri('.grid', {
layout: {
rounding: false,
},
});
window.addEventListener('load', () => {
grid.refreshItems().layout();
document.getElementById('grid').classList.add('uploaded-images');
});
links.forEach((link) => {
link.addEventListener('click', (e) => {
e.preventDefault();
// remove active class for others links
links.forEach((l) => {
l.classList.remove('active');
});
// set class active to clicked link
e.target.classList.add('active');
const category = e.target.innerHTML.toLowerCase();
// filter by category
if (category !== 'all') {
grid.filter(`[data-category="${category}"]`);
} else {
grid.filter(`[data-category]`);
}
});
});
searchBar.addEventListener('input', (e) => {
const label = e.target.value.toLowerCase();
grid.filter((item) => item.getElement().dataset.labels.includes(label));
});
items.forEach((item) => {
const url = item.getAttribute('src');
const desc = item.parentElement.parentElement.getAttribute('data-desc');
item.addEventListener('click', () => {
overlay.classList.add('active');
document.querySelector('#overlay img').src = url;
document.querySelector('#overlay p').innerHTML = desc;
});
});
document.querySelector('#close-popup').addEventListener('click', () => {
overlay.classList.remove('active');
});
overlay.addEventListener('click', (e) => {
if (e.target.id === 'overlay') overlay.classList.remove('active');
});