JSFiddle - React, Tailwind, and code Playground
by chayacooper
HTML
<div id="container">
<ul id="filterOptions">
<li class="active"><a href="#" class="All" data-color="All">All</a></li>
<li><a href="#" class="Blue" data-color="Blue">Blue</a></li>
<li><a href="#" class="Red" data-color="Red">Red</a></li>
<li><a href="#" class="Black" data-color="Black">Black</a></li>
<li><a href="#" class="White" data-color="White">White</a></li>
</ul>
<div id="content">
<ul>
<li class="item Blue" data-color="Blue">Blue 1</li>
<li class="item Blue" data-color="Blue">Blue 2</li>
<li class="item Red" data-color="Red">Red 1</li>
<li class="item Red" data-color="Red">Red 2</li>
<li class="item Black" data-color="Black">Black 1</li>
<li class="item Black" data-color="Black">Black 2</li>
<li class="item White" data-color="White">White 1</li>
<li class="item White" data-color="White">White 2</li>
</ul>
</div>
</div>
CSS
ol, ul {
list-style: none;
}
ul#filterOptions {
width: 802px;
height: 52px;
margin: 30px 0;
overflow: hidden;
}
ul#filterOptions li {
height: 52px;
margin-right: 2px;
float: left;
}
ul#filterOptions li a {
height: 50px;
padding: 0 20px;
border: 1px solid #999;
background: #cfcfcf;
color: #fff;
font-weight: bold;
line-height: 50px;
text-decoration: none;
display: block;
}
ul#filterOptions li a:hover {
background: #c9c9c9;
}
ul#filterOptions li.active a {
background: #999;
}
JavaScript
$(document).ready(function () {
$('#filterOptions li a').click(function () {
// fetch the class of the clicked item
var ourClass = $(this).attr('class');
// reset the active class on all the buttons
$('#filterOptions li').removeClass('active');
// update the active state on our clicked button
$(this).parent().addClass('active');
if (ourClass == 'All') {
// show all our items
$('#content').find('.item').show();
} else {
// hide all elements that don't share ourClass
$('#content').find('.item:not(.' + ourClass + ')').hide();
// show all elements that do share ourClass
$('#content').find('.item.' + ourClass).show();
}
return false;
});
});