<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 */
/* Declaring the alternative text for each image file */
const images = [
{
src: 'https://cdn.glitch.global/3efa4dc0-c1aa-4ccc-a660-baa79fe0f8ec/pic1.jpg?v=1662647926542',
alt: 'Closeup of a human eye'
},
{
src: 'https://cdn.glitch.global/3efa4dc0-c1aa-4ccc-a660-baa79fe0f8ec/pic2.jpg?v=1662647931322',
alt: 'Wavy surface'
},
{
src: 'https://cdn.glitch.global/3efa4dc0-c1aa-4ccc-a660-baa79fe0f8ec/pic3.jpg?v=1662647935495',
alt: 'Purple flowers'
},
{
src: 'https://cdn.glitch.global/3efa4dc0-c1aa-4ccc-a660-baa79fe0f8ec/pic4.jpg?v=1662647939623',
alt: 'Ancient art on a wall'
},
{
src: 'https://cdn.glitch.global/3efa4dc0-c1aa-4ccc-a660-baa79fe0f8ec/pic5.jpg?v=1662647943671',
alt: 'A butterfly on a leaf'
}
]
/* Looping through images */
// Using dot notation:
for (const image of images) {
const newImage = document.createElement('img');
newImage.src = image.src;
newImage.alt = image.alt;
thumbBar.appendChild(newImage);
newImage.addEventListener('click', ()=>{
displayedImage.src = image.src;
displayedImage.alt = image.alt
})
}
/* Using setAttribute/getAttribute:
for (const image of images) {
const newImage = document.createElement('img');
newImage.setAttribute('src', image.src);
newImage.setAttribute('alt', image.alt);
thumbBar.appendChild(newImage);
newImage.addEventListener('click', ()=>{
displayedImage.setAttribute('src', image.src);
displayedImage.setAttribute('alt', image.alt)
})
}
*/
/* Wiring up the Darken/Lighten button */
// Using dot notation:
btn.addEventListener('click', ()=>{
if(btn.className === 'dark' ){
btn.className = 'light';
btn.textContent = 'Lighten';
overlay.style.backgroundColor = 'rgba(0,0,0,0.5)'
} else {
btn.className = 'dark';
btn.textContent = 'Darken';
overlay.style.backgroundColor = 'rgba(0,0,0,0)'
}
})
/* Using setAttribute/getAttribute:
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)'
}
})
*/
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.