Filter - Multiple Filters - MixItUp
by bizamajig
HTML
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<div class="controls">
<label>Filter:</label>
<button class="filter active pre-toggle" data-filter=".category-1">Category 1</button>
<button class="filter active pre-toggle" data-filter=".category-2">Category 2</button>
<button class="filter active pre-toggle" data-filter=".category-3">Category 3</button>
<button class="filter active pre-toggle" data-filter=".category-4">Category 4</button>
<button id="Reset" class="fix-active">Show All</button>
<label>Sort:</label>
<button class="sort" data-sort="name:asc">Asc</button>
<button class="sort" data-sort="name:desc">Desc</button>
</div>
<div id="Container" class="container">
<!-- funky data-name values are to see how well it handles foreign characters (though hopefully we wouldn't ever pass them into the attribute) -->
<div class="mix category-1" data-name="a./33-1"></div>
<div class="mix category-1" data-name="a.,33-1"></div>
<div class="mix category-4" data-name="ca-4"></div>
<div class="mix category-2" data-name="ca-3"></div>
<div class="mix category-1" data-name="e-4"></div>
<div class="mix category-1" data-name="f-5"></div>
<div class="mix category-2" data-name="g-s"></div>
<div class="mix category-2" data-name="h-f"></div>
<div class="mix category-3" data-name="i-a"></div>
<div class="mix category-3" data-name="j-b"></div>
<div class="mix category-3" data-name="k-c"></div>
<div class="mix category-4" data-name="l-d"></div>
<div class="mix category-3" data-name="m-e"></div>
<div class="mix category-4" data-name="n-f"></div>
<div class="mix category-4" data-name="o-g"></div>
<div class="gap"></div>
<div class="gap"></div>
</div>
CSS
*{
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body, button{
font-family: 'Helvetica Neue', arial, sans-serif;
}
.controls{
padding: 2%;
background: #333;
color: #eee;
}
label{
font-weight: 300;
margin: 0 .4em 0 0;
}
button{
display: inline-block;
padding: .4em .8em;
background: #666;
border: 0;
color: #ddd;
font-size: 16px;
font-weight: 300;
border-radius: 4px;
cursor: pointer;
}
button.active{
background: #68b8c4;
}
button:focus{
outline: 0 none;
}
button + label{
margin-left: 1em;
}
.container{
padding: 2% 2% 0;
text-align: justify;
font-size: 0.1px;
background: #68b8c4;
-webkit-backface-visibility: hidden;
}
.container:after{
content: '';
display: inline-block;
width: 100%;
}
.container .mix,
.container .gap{
display: inline-block;
width: 49%;
font-size:40px;
padding: 35px;
color:#fff;
text-transform: uppercase;
}
.container .mix{
text-align: left;
background: #03899c;
margin-bottom: 2%;
display: none;
}
.container .mix.category-1{
border-top: 2px solid limegreen;
}
.container .mix.category-2{
border-top: 2px solid yellow;
}
.container .mix.category-3{
border-top: 2px solid white;
}
.container .mix.category-4{
border-top: 2px solid red;
}
.container .mix:after{
content: attr(data-name);
color: white;
font-size: 36px;
display: inline-block;
vertical-align: top;
text-align:center;
width: 100%;
font-weight: 700;
}
.container .mix:before{
content: '';
display: inline-block;
padding-top: 60%;
}
.fix-active {
background: #68b8c4;
}
.active.pre-toggle {
background: #666;
}
@media all and (min-width: 420px){
.container .mix,
.container .gap{
width: 32%;
}
}
@media all and (min-width: 640px){
.container .mix,
.container .gap{
width: 23.5%;
}
}
JavaScript
$(function(){
// Declare vars, cache jQuery objects
var $container = $('#Container'),
$controls = $('.controls'),
$reset = $('#Reset'),
$sortButtons = $controls.find('.sort'),
all = ".category-1,.category-2,.category-3,.category-4";
// MixItUp in instantiated with toggling enabled, but controls disabled, as the first filter click must be handled through the API
$container.mixItUp({
load: {
filter: all
},
controls: {
toggleFilterButtons: true,
enable: false
},
callbacks: {
onMixEnd: function(state) {
console.log(state);
}
}
});
// This function binds the "initial" behaviour only, where all active are toggled off apart from the clicked filter.
var bindInitial = function(){
$controls.on('click.initial', '.filter', function(){
var $button = $(this),
filter = $button.attr('data-filter');
// Remove active class from all other filers
$button.removeClass('pre-toggle');
$button.siblings().removeClass('active pre-toggle fix-active');
// Unbind the intial behaviour
$controls.off('.initial');
// Filter the clicked category using the API, enable default controls, and finally rebind MixItUp's default clickable controls using the private '_bindHandlers' method
$container
.mixItUp('filter', filter)
.mixItUp('setOptions',{
controls: {
enable: true
}
})
.mixItUp('_bindHandlers');
});
// MixItUp will behave as normal from now one.
};
// Bind sort buttons
$sortButtons.on('click', function() {
$sortButton = $(this);
$container.mixItUp('sort', $sortButton.attr('data-sort'));
});
// Bind reset funcionality:
$reset.on('click', function(){
$reset.addClass('fix-active');
// Filter all categories back in, unbind MixItUp's default controls, and...