A Pen by Dave DeSandro on CodePen.
Created
July 15, 2022 12:01
-
-
Save potat-dev/4ead4cdcc8565d9b8010ced3f6d99a78 to your computer and use it in GitHub Desktop.
Masonry - stagger option, vanilla JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Masonry - stagger option, vanilla JS</h1> | |
<p>Resize window or click to toggle item size</p> | |
<div class="grid"> | |
<div class="grid-item"></div> | |
<div class="grid-item grid-item--width2 grid-item--height2"></div> | |
<div class="grid-item grid-item--height3"></div> | |
<div class="grid-item grid-item--height2"></div> | |
<div class="grid-item grid-item--width3"></div> | |
<div class="grid-item"></div> | |
<div class="grid-item"></div> | |
<div class="grid-item grid-item--height2"></div> | |
<div class="grid-item grid-item--width2 grid-item--height3"></div> | |
<div class="grid-item"></div> | |
<div class="grid-item grid-item--height2"></div> | |
<div class="grid-item"></div> | |
<div class="grid-item grid-item--width2 grid-item--height2"></div> | |
<div class="grid-item grid-item--width2"></div> | |
<div class="grid-item"></div> | |
<div class="grid-item grid-item--height2"></div> | |
<div class="grid-item"></div> | |
<div class="grid-item"></div> | |
<div class="grid-item grid-item--height3"></div> | |
<div class="grid-item grid-item--height2"></div> | |
<div class="grid-item"></div> | |
<div class="grid-item"></div> | |
<div class="grid-item grid-item--height2"></div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// external js: masonry.pkgd.js | |
var grid = document.querySelector('.grid'); | |
var msnry = new Masonry( grid, { | |
itemSelector: '.grid-item', | |
columnWidth: 160, | |
stagger: 30, | |
}); | |
grid.addEventListener( 'click', function( event ) { | |
// don't proceed if item was not clicked on | |
if ( !matchesSelector( event.target, '.grid-item' ) ) { | |
return; | |
} | |
// change size of item via class | |
event.target.classList.toggle('grid-item--gigante'); | |
// trigger layout | |
msnry.layout(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://npmcdn.com/masonry-layout@4/dist/masonry.pkgd.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* { box-sizing: border-box; } | |
body { font-family: sans-serif; } | |
/* ---- grid ---- */ | |
.grid { | |
background: #EEE; | |
max-width: 1200px; | |
} | |
/* clearfix */ | |
.grid:after { | |
content: ''; | |
display: block; | |
clear: both; | |
} | |
/* ---- grid-item ---- */ | |
.grid-item { | |
width: 160px; | |
height: 120px; | |
float: left; | |
background: #D26; | |
border: 2px solid #333; | |
border-color: hsla(0, 0%, 0%, 0.5); | |
border-radius: 5px; | |
} | |
.grid-item--width2 { width: 320px; } | |
.grid-item--width3 { width: 480px; } | |
.grid-item--width4 { width: 640px; } | |
.grid-item--height2 { height: 200px; } | |
.grid-item--height3 { height: 260px; } | |
.grid-item--height4 { height: 360px; } | |
.grid-item--gigante { | |
width: 320px; | |
height: 360px; | |
z-index: 2; /* above other items */ | |
} | |
.grid-item:hover { | |
background: #A2C; | |
border-color: white; | |
cursor: pointer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment