Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 94 additions & 19 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,137 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="styles.css" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<link
rel="stylesheet"
href="styles.css"
/>
<title>JavaScript Photo Gallery by Codebase Mentor</title>
</head>
<body>
<div class="gallery">
<div class="grid-wrapper">
<div>
<img class="image" src="../public/image.jpg" alt="" />
<img
class="image"
src="../public/image.jpg"
alt=""
/>
</div>
<div>
<img class="image" src="../public/image-1.jpg" alt="" />
<img
class="image"
src="../public/image-1.jpg"
alt=""
/>
</div>
<div>
<img class="image" src="../public/image-2.jpg" alt="" />
<img
class="image"
src="../public/image-2.jpg"
alt=""
/>
</div>
</div>
<div class="grid-wrapper">
<div>
<img class="image" src="../public/image-3.jpg" alt="" />
<img
class="image"
src="../public/image-3.jpg"
alt=""
/>
</div>
<div>
<img class="image" src="../public/image-4.jpg" alt="" />
<img
class="image"
src="../public/image-4.jpg"
alt=""
/>
</div>
<div>
<img class="image" src="../public/image-5.jpg" alt="" />
<img
class="image"
src="../public/image-5.jpg"
alt=""
/>
</div>
</div>
<div class="grid-wrapper">
<div>
<img class="image" src="../public/image-6.jpg" alt="" />
<img
class="image"
src="../public/image-6.jpg"
alt=""
/>
</div>
<div>
<img class="image" src="../public/image-7.jpg" alt="" />
<img
class="image"
src="../public/image-7.jpg"
alt=""
/>
</div>
<div>
<img class="image" src="../public/image-8.jpg" alt="" />
<img
class="image"
src="../public/image-8.jpg"
alt=""
/>
</div>
</div>
<div class="grid-wrapper">
<div>
<img class="image" src="../public/image-9.jpg" alt="" />
<img
class="image"
src="../public/image-9.jpg"
alt=""
/>
</div>
<div>
<img class="image" src="../public/image-10.jpg" alt="" />
<img
class="image"
src="../public/image-10.jpg"
alt=""
/>
</div>
<div>
<img class="image" src="../public/image-11.jpg" alt="" />
<img
class="image"
src="../public/image-11.jpg"
alt=""
/>
</div>
</div>
</div>
<div id="modal" class="modal hidden">
<span id="closeBtn" class="close">&times;</span>
<img id="modalImage" class="modal-image" alt="" />
<button id="prevBtn" class="nav-btn prev-btn">&lt;</button>
<button id="nextBtn" class="nav-btn next-btn">&gt;</button>
<div
id="modal"
class="modal hidden"
>
<span
id="closeBtn"
class="close"
>&times;</span
>
<img
id="modalImage"
class="modal-image"
alt=""
/>
<button
id="prevBtn"
class="nav-btn prev-btn"
>
&lt;
</button>
<button
id="nextBtn"
class="nav-btn next-btn"
>
&gt;
</button>
</div>
<script src="scripts.js"></script>
</body>
Expand Down
46 changes: 36 additions & 10 deletions src/scripts.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,47 @@
document.addEventListener("DOMContentLoaded", () => {
const modal = document.getElementById("modal");
const modalImage = document.getElementById("modalImage");
const closeBtn = document.getElementById("closeBtn");
const prevBtn = document.getElementById("prevBtn");
const nextBtn = document.getElementById("nextBtn");
document.addEventListener('DOMContentLoaded', () => {
const modal = document.getElementById('modal')
const modalImage = document.getElementById('modalImage')
const closeBtn = document.getElementById('closeBtn')
const prevBtn = document.getElementById('prevBtn')
const nextBtn = document.getElementById('nextBtn')

const images = Array.from(document.querySelectorAll(".image"));
let currentImageIndex = 0;
const images = Array.from(document.querySelectorAll('.image'))
let currentImageIndex = 0

// TODO: (Issue #1) Add an event listener for each image in the gallery to show it in the modal when clicked
images.forEach((element) => {
element.addEventListener('click', () => {
modal.classList.remove('hidden')
setCurrentImg(images.indexOf(element))
})
})

// TODO: (Issue #2) Add event listeners for the prevBtn and nextBtn for modal navigation functionality
// Clean Code Hint: Issue #1 and #2 both deal with updating the modal image based on currentImageIndex. Consider creating a function to handle this functionality.
nextBtn.addEventListener('click', () => setCurrentImg(currentImageIndex + 1))
prevBtn.addEventListener('click', () => setCurrentImg(currentImageIndex - 1))

// TODO: (Issue #3) Add an event listener for the closeBtn to close the modal
closeBtn.addEventListener('click', () => modal.classList.add('hidden'))

// TODO: (Issue #3) Add an event listener to close the modal when clicking the modal background
modal.addEventListener('click', (event) => {
if (event.target === modal) {
modal.classList.add('hidden')
}
})

// TODO: (Issue #4) Create a function to update the state of the navigation buttons based on currentImageIndex
});
function setBtnDisabled() {
if (currentImageIndex === 0) prevBtn.disabled = true
else prevBtn.disabled = false
if (currentImageIndex === images.length - 1) nextBtn.disabled = true
else nextBtn.disabled = false
}

// Clean Code Hint: Issue #1 and #2 both deal with updating the modal image based on currentImageIndex. Consider creating a function to handle this functionality.
function setCurrentImg(i) {
modalImage.src = images[i].src
currentImageIndex = i
setBtnDisabled()
}
})