<html lang="en-us">
<head>
<meta charset="utf-8">
<title>Image gallery</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Image gallery example</h1>
<div class="full-img">
<img class="displayed-img" src="https://cdn.glitch.global/3efa4dc0-c1aa-4ccc-a660-baa79fe0f8ec/pic1.jpg?v=1662647926542" alt="Closeup of a human eye">
<div class="overlay"></div>
<button class="dark">Darken</button>
</div>
<div class="thumb-bar">
</div>
<script src="main.js"></script>
</body>
</html>
h1 {
font-family: helvetica, arial, sans-serif;
text-align: center;
}
body {
width: 640px;
margin: 0 auto;
}
.full-img {
position: relative;
display: block;
width: 640px;
height: 480px;
}
.overlay {
position: absolute;
top: 0;
left: 0;
width: 640px;
height: 480px;
background-color: rgba(0,0,0,0);
}
button {
border: 0;
background: rgba(150,150,150,0.6);
text-shadow: 1px 1px 1px white;
border: 1px solid #999;
position: absolute;
cursor: pointer;
top: 2px;
left: 2px;
}
.thumb-bar img {
display: block;
width: 20%;
float: left;
cursor: pointer;
}
const displayedImage = document.querySelector('.displayed-img');
const thumbBar = document.querySelector('.thumb-bar');
const btn = document.querySelector('button');
const overlay = document.querySelector('.overlay');
/* Declaring the array of image filenames */
const images = ['pic1.jpg', 'pic2.jpg', 'pic3.jpg', 'pic4.jpg', 'pic5.jpg'];
/* Declaring the alternative text for each image file */
// How was I supposed to use this???
/* Looping through images */
for (const image of images) {
const newImage = document.createElement('img');
newImage.setAttribute('src', 'xxx');
newImage.setAttribute('alt', 'xxx');
thumbBar.appendChild(newImage);
switch (image) {
case 'pic1.jpg':
newImage.src = 'https://cdn.glitch.global/3efa4dc0-c1aa-4ccc-a660-baa79fe0f8ec/pic1.jpg?v=1662647926542';
newImage.alt = 'Closeup of a human eye';
break;
case 'pic2.jpg':
newImage.src = 'https://cdn.glitch.global/3efa4dc0-c1aa-4ccc-a660-baa79fe0f8ec/pic2.jpg?v=1662647931322';
newImage.alt = 'Wavy surface';
break;
case 'pic3.jpg':
newImage.src = 'https://cdn.glitch.global/3efa4dc0-c1aa-4ccc-a660-baa79fe0f8ec/pic3.jpg?v=1662647935495';
newImage.alt = 'Purple flowers';
break;
case 'pic4.jpg':
newImage.src = 'https://cdn.glitch.global/3efa4dc0-c1aa-4ccc-a660-baa79fe0f8ec/pic4.jpg?v=1662647939623';
newImage.alt = 'Ancient art on a wall';
break;
case 'pic5.jpg':
newImage.src = 'https://cdn.glitch.global/3efa4dc0-c1aa-4ccc-a660-baa79fe0f8ec/pic5.jpg?v=1662647943671';
newImage.alt = 'A butterfly on a leaf';
break;
};
thumbBar.addEventListener('click', (event)=> {
const currentSrc = event.target.getAttribute('src');
const currentAlt = event.target.getAttribute('alt');
displayedImage.setAttribute('src', currentSrc);
displayedImage.setAttribute('alt', currentAlt);
});
}
/* Wiring up the Darken/Lighten button */
btn.addEventListener('click', () => {
if (btn.getAttribute('class') === 'dark') {
btn.setAttribute('class', 'light');
btn.textContent = 'Lighten';
overlay.style.backgroundColor = 'rgba(0,0,0,0.5)';
} else {
btn.setAttribute('class','dark');
btn.textContent = 'Darken';
overlay.style.backgroundColor = 'rgba(0,0,0,0)';
}
})
/* NOTE for myself
- I forgot to put const in for...of () so it didn't work.
- I didn't put quote marks at "case 'picx.jpg':" so the photos weren't displayed on thumbBar.
- It took a while but I could figure out how to add click event handler only to thumbBar!
To get the src and alt of the 'current image', target "event.target" so it targets innermost element.
*/
/* Alternative src & alt Switch
switch (image) {
case 'pic1.jpg':
newImage.setAttribute('src','images/pic1.jpg');
newImage.setAttribute('alt', 'Closeup of a human eye');
thumbBar.appendChild(newImage);
break;
case 'pic2.jpg':
newImage.setAttribute('src','images/pic2.jpg');
newImage.setAttribute('alt','Wavy surface');
thumbBar.appendChild(newImage);
break;
case 'pic3.jpg':
newImage.setAttribute('src','images/pic3.jpg');
newImage.setAttribute('alt','Purple flowers');
thumbBar.appendChild(newImage);
break;
case 'pic4.jpg':
newImage.setAttribute('src','images/pic4.jpg');
newImage.setAttribute('alt','Ancient art on a wall');
thumbBar.appendChild(newImage);
break;
case 'pic5.jpg':
newImage.setAttribute('src','images/pic5.jpg');
newImage.setAttribute('alt','A butterfly on a leaf');
thumbBar.appendChild(newImage);
break;
};
*/
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.