-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
71 lines (59 loc) · 1.73 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
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
67
68
69
70
71
let myImgs = [
"./img/architecture.jpg",
"./img/athens.jpg",
"./img/akropolis.jpg",
"./img/beach.jpg",
"./img/cat.jpg",
"./img/door.jpg",
"./img/flower.jpg",
"./img/house.jpg",
"./img/orthodox.jpg",
"./img/santorini.jpg",
"./img/sunset.jpg",
"./img/stairs.jpg",
];
let currentIndex = 0;
// oder render ()
function init() {
const gallery = document.getElementById("gallery");
let html = "";
//einfacher als myImgs.forEach(..,..)
for (let i = 0; i < myImgs.length; i++) {
html += `
<div class="gallery-item">
<img src="${myImgs[i]}" alt="Image ${i + 1}
"onclick="openModal(${i})">
</div>`;
}
gallery.innerHTML = html;
}
function openModal(index) {
currentIndex = index;
const modal = document.getElementById("imageModal");
const modalImage = document.getElementById("modalImage");
modalImage.src = myImgs[currentIndex];
modal.style.display = "flex";
}
function closeModal() {
const modal = document.getElementById("imageModal");
modal.style.display = "none";
}
function nextImage() {
currentIndex = (currentIndex + 1) % myImgs.length;
document.getElementById("modalImage").src = myImgs[currentIndex];
}
function prevImage() {
currentIndex = (currentIndex - 1 + myImgs.length) % myImgs.length;
document.getElementById("modalImage").src = myImgs[currentIndex];
}
// closeModal Funktion
function handleModalClick(event) {
const modal = document.getElementById("imageModal");
// Prüft, ob der Klick außerhalb des Bildes erfolgte
if (event.target === modal) {
modal.style.display = "none";
}
}
document
.getElementById("imageModal")
.addEventListener("click", handleModalClick);