forked from leslie-alldridge/Hacktoberfest-2023-Cats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
35 lines (34 loc) · 1.24 KB
/
script.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
var gallery = document.querySelector('#gallery');
var getVal = function (elem, style) {
return parseInt(window.getComputedStyle(elem).getPropertyValue(style));
};
var getHeight = function (item) {
return item.querySelector('.content').getBoundingClientRect().height;
};
var resizeAll = function () {
var altura = getVal(gallery, 'grid-auto-rows');
var gap = getVal(gallery, 'grid-row-gap');
gallery.querySelectorAll('.gallery-item').forEach(function (item) {
var el = item;
el.style.gridRowEnd =
'span ' + Math.ceil((getHeight(item) + gap) / (altura + gap));
});
};
gallery.querySelectorAll('img').forEach(function (item) {
item.addEventListener('load', function () {
var altura = getVal(gallery, 'grid-auto-rows');
var gap = getVal(gallery, 'grid-row-gap');
var gitem = item.parentElement.parentElement;
gitem.style.gridRowEnd =
'span ' + Math.ceil((getHeight(gitem) + gap) / (altura + gap));
item.classList.remove('byebye');
});
});
window.addEventListener('resize', resizeAll);
gallery.querySelectorAll('.gallery-item').forEach(function (item) {
item.addEventListener('click', function () {
item.classList.toggle('full');
});
});
// resize all images on page load - prevents bug where images sometimes overlap
window.onload = resizeAll;