Skip to content

Instantly share code, notes, and snippets.

@patkub
Last active June 14, 2018 06:56
Show Gist options
  • Save patkub/660579531c045ff1977fee1861feda37 to your computer and use it in GitHub Desktop.
Save patkub/660579531c045ff1977fee1861feda37 to your computer and use it in GitHub Desktop.
Make masonry compatible with materializecss' material box. Masonry requires imagesLoaded.
var msnry, openIMG;
imagesLoaded(grid, function() {
// init Isotope after all images have loaded
var $container = $('#grid').masonry({
itemSelector: '.grid-item',
columnWidth: '.grid-sizer',
percentPosition: true
});
// allow image to open full size
$container.on('click', '.grid-item', function() {
// add open-img class and remove grid-item class
$(this).addClass("open-img");
$(this).removeClass("grid-item");
openIMG = this;
});
// return image back to grid on container click
$container.on('click', '.open-img', function() {
// add grid-item class back in and remove open-img class
$(this).removeClass("open-img");
$(this).addClass("grid-item");
});
// return image back to grid on click
$("img").click(function(){
if ($(this).hasClass("materialboxed"))
{
$(this).click(function(){
$(openIMG).removeClass("open-img");
$(openIMG).addClass("grid-item");
});
}
});
// return image back to grid on scroll
$(window).scroll(function() {
if ($("img").hasClass("materialboxed"))
{
$(openIMG).removeClass("open-img");
$(openIMG).addClass("grid-item");
}
});
// return image back to grid on ESC
$(document).keyup(function(e) {
if (e.keyCode === 27) {
if ($("img").hasClass("materialboxed")) {
$(openIMG).removeClass("open-img");
$(openIMG).addClass("grid-item");
}
}
});
});
/* clear fix */
.grid:after {
content: '';
display: block;
clear: both;
}
/* .grid-item */
.grid-sizer,
.grid-item {
width: 100%;
}
@media (min-width: 768px) {
.grid-sizer,
.grid-item {
width: 33.333%;
}
}
.grid-item {
float: left;
}
.grid-item img {
display: block;
max-width: 100%;
}
<div id="grid" class="grid">
<div class="grid-sizer"></div>
<div class="grid-item">
<img class="materialboxed" data-caption="lorem" src="image.jpg">
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment