-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
55 lines (48 loc) · 1.85 KB
/
scripts.js
File metadata and controls
55 lines (48 loc) · 1.85 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
class MediaManager {
static currentlyActive = null; // Shared across all instances
constructor(containerSelector) {
this.containers = document.querySelectorAll(containerSelector);
this.initEvents();
}
initEvents() {
this.containers.forEach(container => {
container.addEventListener('click', event => {
const target = event.target;
if (target.tagName === 'IMG' || target.tagName === 'VIDEO') {
if (target === MediaManager.currentlyActive) {
this.closeMedia(); // Minimize if the same media is clicked
} else {
this.expandMedia(target); // Expand new media
}
event.stopPropagation();
}
});
});
document.addEventListener('click', () => {
this.closeMedia();
});
}
expandMedia(media) {
// Close any previously active media
this.closeMedia();
// Add 'expanded' class to clicked media and set it as currently active globally
media.classList.add('expanded');
if (media.tagName === 'VIDEO') {
media.play(); // Autoplay when expanded
}
MediaManager.currentlyActive = media;
}
closeMedia() {
// If media is active, remove the class and reset the currently active media
if (MediaManager.currentlyActive) {
if (MediaManager.currentlyActive.tagName === 'VIDEO' && !MediaManager.currentlyActive.paused) {
MediaManager.currentlyActive.pause();
}
MediaManager.currentlyActive.classList.remove('expanded');
MediaManager.currentlyActive = null;
}
}
}
document.addEventListener('DOMContentLoaded', () => {
new MediaManager('.gallery-container');
});