Skip to content

Instantly share code, notes, and snippets.

@ahmedtalaltwd7
Created July 21, 2024 23:27
Show Gist options
  • Save ahmedtalaltwd7/695be5e98a1122cc64a53662f72a4c43 to your computer and use it in GitHub Desktop.
Save ahmedtalaltwd7/695be5e98a1122cc64a53662f72a4c43 to your computer and use it in GitHub Desktop.
Responsive Grid Image Gallery ( Animation )
<main class='gallery'>
<div class='item' data-pos='0'><img src='https://images.unsplash.com/photo-1708247874023-f6d71a45113a?q=80&w=2344&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D'></div>
<div class='item' data-pos='1'><img src='https://images.unsplash.com/photo-1437751059337-ea72d4f73fcf?q=80&w=2322&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D'></div>
<div class='item' data-pos='2'><img src='https://images.unsplash.com/photo-1515594515116-863345d8507c?q=80&w=2340&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D'></div>
<div class='item' data-pos='3'><img src='https://images.unsplash.com/photo-1533106497176-45ae19e68ba2?q=80&w=2340&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D'></div>
<div class='item' data-pos='4'><img src='https://images.unsplash.com/photo-1517953377824-516f2dca803b?q=80&w=2378&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D'></div>
</main>
function init(e) {
if (!e.target.closest('.item')) return;
let hero = document.querySelector('div[data-pos="0"]');
let target = e.target.parentElement;
[target.dataset.pos,hero.dataset.pos] = [hero.dataset.pos,target.dataset.pos];
}
window.addEventListener('click',init,false);
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
height: 100vh;
height: 100svh;
display: grid;
place-items: center;
}
.gallery {
position: relative;
width: 760px;
height: 305px;
&:hover :not(div[data-pos='0'],img) { cursor: pointer; }
& .item {
position: absolute;
width: 175px;
height: 150px;
overflow: hidden;
transition: transform 0.9s, width 0.9s, height 0.9s;
& img {
width: 100%;
height: 100%;
}
}
& div[data-pos='0'] {
width: 400px;
height: inherit;
z-index: 10;
}
}
div[data-pos='0'] { transform: translate(0,0); }
div[data-pos='1'] { transform: translate(405px,0); }
div[data-pos='2'] { transform: translate(585px,0); }
div[data-pos='3'] { transform: translate(405px,155px); }
div[data-pos='4'] { transform: translate(585px,155px); }
@media (width < 800px) {
.gallery {
width: 405px;
height: 660px;
& .item {
width: 200px;
height: 150px;
}
& div[data-pos='0'] {
width: inherit;
height: 350px;
z-index: 10;
}
}
div[data-pos='0'] { transform: translate(0,0); }
div[data-pos='1'] { transform: translate(0,355px); }
div[data-pos='2'] { transform: translate(0,510px); }
div[data-pos='3'] { transform: translate(205px,355px); }
div[data-pos='4'] { transform: translate(205px,510px); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment