Created
May 1, 2018 15:08
-
-
Save chwzr/7867aa3f27fc7e23c6542f53afce2f1b to your computer and use it in GitHub Desktop.
Masonry - imagesLoaded progress, vanilla JS
This file contains hidden or 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 - imagesLoaded progress, vanilla JS</h1> | |
<div class="grid"> | |
<div class="grid-sizer"></div> | |
<div class="grid-item"> | |
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/orange-tree.jpg" /> | |
</div> | |
<div class="grid-item"> | |
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/submerged.jpg" /> | |
</div> | |
<div class="grid-item"> | |
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/look-out.jpg" /> | |
</div> | |
<div class="grid-item"> | |
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/one-world-trade.jpg" /> | |
</div> | |
<div class="grid-item"> | |
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/drizzle.jpg" /> | |
</div> | |
<div class="grid-item"> | |
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/cat-nose.jpg" /> | |
</div> | |
<div class="grid-item"> | |
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/contrail.jpg" /> | |
</div> | |
<div class="grid-item"> | |
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/golden-hour.jpg" /> | |
</div> | |
<div class="grid-item"> | |
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/flight-formation.jpg" /> | |
</div> | |
</div> |
This file contains hidden or 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, imagesloaded.pkgd.js | |
// init Masonry | |
var grid = document.querySelector('.grid'); | |
var msnry = new Masonry( grid, { | |
itemSelector: '.grid-item', | |
columnWidth: '.grid-sizer', | |
percentPosition: true | |
}); | |
imagesLoaded( grid ).on( 'progress', function() { | |
// layout Masonry after each image loads | |
msnry.layout(); | |
}); |
This file contains hidden or 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://masonry.desandro.com/masonry.pkgd.js"></script> | |
<script src="https://imagesloaded.desandro.com/imagesloaded.pkgd.js"></script> |
This file contains hidden or 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; } | |
/* force scrollbar */ | |
html { overflow-y: scroll; } | |
body { font-family: sans-serif; } | |
/* ---- grid ---- */ | |
.grid { | |
background: #fff; | |
} | |
/* clear fix */ | |
.grid:after { | |
content: ''; | |
display: block; | |
clear: both; | |
} | |
/* ---- .grid-item ---- */ | |
.grid-sizer, | |
.grid-item { | |
width: 25%; | |
} | |
.grid-item { | |
float: center; | |
} | |
.grid-item img { | |
display: block; | |
width: 100%; | |
padding: 10px; | |
} | |
@media(max-width: 1200px){ | |
.grid-sizer, | |
.grid-item { | |
width: 50%; | |
} | |
} | |
@media(max-width: 920px){ | |
.grid-sizer, | |
.grid-item { | |
width: 100%; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment