Skip to content

Instantly share code, notes, and snippets.

@desandro
Created March 1, 2015 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save desandro/001c23852ce6cd552028 to your computer and use it in GitHub Desktop.
Save desandro/001c23852ce6cd552028 to your computer and use it in GitHub Desktop.
function getHashFilter() {
var hash = location.hash;
// get filter=filterName
var matches = location.hash.match( /filter=([^&]+)/i );
var hashFilter = matches && matches[1];
return hashFilter && decodeURIComponent( hashFilter );
}
$( function() {
var $container = $('.isotope');
// bind filter button click
var $filters = $('#filters').on( 'click', 'button', function() {
var filterAttr = $( this ).attr('data-filter');
// set filter in hash
location.hash = 'filter=' + encodeURIComponent( filterAttr );
});
var isIsotopeInit = false;
function onHashchange() {
var hashFilter = getHashFilter();
if ( !hashFilter && isIsotopeInit ) {
return;
}
isIsotopeInit = true;
// filter isotope
$container.isotope({
itemSelector: '.item',
layoutMode: 'packery',
filter: hashFilter
});
// set selected class on button
if ( hashFilter ) {
$filters.find('.is-checked').removeClass('is-checked');
$filters.find('[data-filter="' + hashFilter + '"]').addClass('is-checked');
}
}
$(window).on( 'hashchange', onHashchange );
// trigger event handler to init Isotope
onHashchange();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment