Skip to content

Instantly share code, notes, and snippets.

@rad-hombre
Created December 31, 2019 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rad-hombre/d96d9916ad1929740621667c8a343d11 to your computer and use it in GitHub Desktop.
Save rad-hombre/d96d9916ad1929740621667c8a343d11 to your computer and use it in GitHub Desktop.
Image Changer
div(class='container')
div(class='main--img')
img(src='https://image.ibb.co/cTfhsy/galaxy.jpg' alt='galaxy' class='active')
div(class='images')
img(src='https://image.ibb.co/cTfhsy/galaxy.jpg' alt='')
img(src='https://image.ibb.co/d0rizd/galaxy_arc.jpg' alt='')
img(src='https://image.ibb.co/iP1BkJ/spaceman.jpg' alt='')
const doc = document;
const currentImg = doc.querySelector('.active')
const imgs = doc.querySelectorAll('.images img');
const imgOpacity = 0.5;
imgs[0].style.opacity = imgOpacity;
let thumbPointer = imgs[0];
// Loop through images, add click event
imgs.forEach( img => img.addEventListener('click', clickImage));
function clickImage(e){
// same pic selected
if(e.target === thumbPointer) return;
// Change curr image to src of clicked image
currentImg.src = e.target.src;
// Add fade in class
currentImg.classList.add('image-fade');
// Change opacity
e.target.style.opacity = imgOpacity;
// Reset opacity of last thumbnail
thumbPointer.style.opacity = 1;
// Update thumb Pointer
thumbPointer = e.target;
// Remove fade-in class
setTimeout(() => currentImg.classList.remove('image-fade'), 500);
}
body {
margin: 25px;
padding: 0;
// background: #000;
}
.container {
max-width: 900px;
margin: auto;
border: 4px solid #333;
}
.main--img img,
.images img {
width: 100%;
cursor: pointer;
transition: 0.8s;
}
.images {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 4px;
}
.images img:hover {
opacity: 0.8 !important;
transition: 0.8s;
}
@keyframes fadeIn {
to {
opacity: 1;
}
}
.image-fade {
opacity: 0;
animation: fadeIn 0.6s 1 forwards;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment