Simple Masonry Filter

In this example below you will see how to do a Simple Masonry Filter with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by megatronCGN, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright megatronCGN ©
  • HTML
  • CSS
  • JavaScript
    <div class="filter">
	<button data-filter="reset">reset</button>
	<button data-filter="c">filter c</button>
	<button data-filter="a">filter a</button>
	<button data-filter="b">filter b</button> 
</div>
<ul class="grid"> 
	<li class="grid__item" data-filter="c">
		<img src="//placehold.it/500x500" />
	</li>
	<li class="grid__item" data-filter="b">
		<img src="//placehold.it/300x150" />
	</li>
	<li class="grid__item" data-filter="c b">
		<img src="//placehold.it/500x150" />
	</li>
	<li class="grid__item" data-filter="a">
		<img src="//placehold.it/500x500" />
	</li>
	<li class="grid__item" data-filter="b c">
		<img src="//placehold.it/300x50" />
	</li>
	<li class="grid__item" data-filter="c">
		<img src="//placehold.it/200x150" />
	</li>
	<li class="grid__item" data-filter="a">
		<img src="//placehold.it/500x500" />
	</li>
	<li class="grid__item" data-filter="b a">
		<img src="//placehold.it/300x500" />
	</li>
	<li class="grid__item" data-filter="b">
		<img src="//placehold.it/500x150" />
	</li>
	<li class="grid__item" data-filter="a">
		<img src="//placehold.it/100x100" />
	</li>
	<li class="grid__item" data-filter="c">
		<img src="//placehold.it/300x50" />
	</li>
	<li class="grid__item" data-filter="c">
		<img src="//placehold.it/200x150" />
	</li>
</ul>

/*Downloaded from https://www.codeseek.co/megatronCGN/simple-masonry-filter-RMbWWm */
    * {
	box-sizing: border-box;
}

body {
	padding: 5vh 0;
}

ul {
	margin: 0;
	padding: 0;
	list-style: none;
}

img {
	width: 100%;
	line-height: 0;
}

.filter {
	text-align: center;
}

.grid {
	width: 75vw;
	margin: 0 auto; 
}

.grid__item {
	width: 33%;
	padding: 0 5px;
	margin: 10px 0 0;
}


/*Downloaded from https://www.codeseek.co/megatronCGN/simple-masonry-filter-RMbWWm */
    (function($) {
	var $grid = $('.grid');
	var $gridItem = $('.grid__item');
	
	$grid.masonry({ 
		itemSelector: '.grid__item'
	});
	
	$('button').click(function() {
		var filter = $(this).data('filter');
		 
		if (filter === 'reset') {
			$gridItem.appendTo($grid);
		} else { 
			$gridItem.appendTo($grid)
				.css('top', '')
				.not('[data-filter*="' + filter + '"]')
				.remove();
			}
		
		$grid.masonry("reloadItems").masonry();		
	});
	
}(jQuery));

Comments