Created
March 1, 2015 19:45
-
-
Save desandro/001c23852ce6cd552028 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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