Categories

Close

Design Lunatic

A Continuation of the Isotope Tutorial: Hash History with Isotope

I’ve written several tutorials on how to use isotope effectively and maximize its potential, and today’s post will be part of that series. What exactly does isotope do? It reorders the content on a page to only display what is necessary. The user can usually control this by clicking on links that change what the content is filtered by. But what if the user refreshes the page? Or decides that what the content was previously filtered by worked better, and presses the back button? Then, problems begin. The user has to reorder the content again to get back to where they were. Today’s post aims to solve that problem by using a plugin called jQuery hashchange event. This plugin works with the hash, the part after the url of the page that begins with “#”. It adds back button support to cycle through the hashes, and adds a handy “hashchange” jquery event.

As usual, before we begin on this post, grab a copy of the original isotope tutorial.

HTML

There are no changes from the HTML of the original isotope tutorial.

CSS

Nope, no changes here either. Keep reading.

Jquery

Here’s where the fun starts. We’ll rewrite the jquery from scratch, as there are several changes and optimizations to be made. Before you start writing the code, download the hashchange plugin. Once you’ve included the plugin, start off with jquery’s essential document.ready function.

$(document).ready(function(){

	var $container = $('#content');

});

There’s also a “$container” variable defined, which is just what we’re filtering.

Now, we need to create the code that will deal with appending values to the URL when a link is clicked.

$('#nav a').click(function(){
	var selector = $(this).attr('data-filter');

	location.hash = selector;

	return false;
});

The above code takes the “data-filter” attribute of the link that was just clicked, and appends it the url via the “location.hash” method. Then, there’s a return false, so the link isn’t followed. The above code works fine, but personally, I don’t like what is appended to the url. I think the dot before the category is extra – so let’s get rid of it.

$('#nav a').click(function(){
	var selector = $(this).attr('data-filter');

	var prettyselector = selector.substr(1);
  
	location.hash = prettyselector;

	return false;
});

With this addition, the URL looks cleaner and is nicer to look at. The above code now does exactly what we want it to do.

At this point, we’ll start using the capabilities of the “hashchange” plugin we’ve included.

$(window).hashchange( function(){

});

This is a “hashchange” function. When the hash of the URL is modified, the function fires. We can use this to filter the content as soon as a “hashchange” is detected.

$(window).hashchange( function(){
	var hashfilter = "." + location.hash.substr(1);
	
	$container.isotope({
		filter: hashfilter,
		animationOptions: {
			duration: 750,
			easing: 'linear',
			queue: false,
		}
	});

});

The “hashfilter” variable may seem a bit complex at first, but it’s quite simple. We’re taking the “location.hash”, which is the current hash of the URL, and deleting the first character. Why? Because the first character is a “#”, and we don’t need it. We then append a period to the beginning of a variable. Here’s what the process looks like: #cat1 => cat1 => .cat1.

While it may seem that the “hashchange” function works exactly as it has to, that’s incorrect. There’s one big issue with this – try clicking on the “All” button. Notice how nothing happens? That needs fixing.

$(window).hashchange( function(){
	
	if(location.hash!="#"){
		var hashfilter = "." + location.hash.substr(1);
	}
	else{
		var hashfilter = "*";
	}

	$container.isotope({
		filter: hashfilter,
		animationOptions: {
			duration: 750,
			easing: 'linear',
			queue: false,
		}
	});

});

The “if…else” statement fixes this. If the “location.hash” is NOT equal to a blank, the “hashfilter” is what created earlier. If it IS a blank space, then the “hashfilter” is a “*”, which basically means “All” in isotope.

We’re almost done – there’s just one thing left. If the page doesn’t get refreshed, everything works exactly as it should – but what if the page is refreshed, or is opened in a new window? The code doesn’t know what to filter it by, so it just goes with “*”. This is easy to fix though; we already wrote all the code we need.

$(document).ready(function(){
	var $container = $('#content');
		
	if(location.hash!=""){
		var hashfilter = "." + location.hash.substr(1);
	}
	else{
		var hashfilter = "*";
	}
		
	$container.isotope({
		filter: hashfilter,
		animationOptions: {
			duration: 750,
			easing: 'linear',
			queue: false,
		}
	});

Boom. Copy-pasted from inside the “hashchange” function, and it does exactly what we need to it do. At this point, everything should work perfectly for you. If it doesn’t, just download the files for the post and compare them to yours, but if it does, good job! That’s the end of this tutorial. If you have any questions or comments, feel free to post them below, and I’ll do my best to answer them.

  • Victoria Boarer

    There seems to be problem when you click on the filter links and then click on All again, in IE (I am using version 9), it doesn’t bring them all up again?

    Also I’m having trouble with using isotope in wordpress, it works in all other browsers except IE, I’m wondering if I’m doing something wrong (www.riotcreative.co.uk/dc/) any help would be great.

  • Alexandroid

    Yes, problems with clicking on All again in IE10 – it`s not working.
    Very interesting post, so I`m waiting for continuation of a story.

  • Derick

    Thank for the tutorials! I’m using isotope to filter some content and I’ve used your tutorials to apply hash history to great success. I’ve also been able to highlight the currently active link with your other tutorial on another page. I can’t, however, combine the too. Any suggestions?

    • Alexandre Smirnov

      Follow the hash history tutorial completely, and once you have that set up and working, do the following:
      In the “active cat” tutorial, take a look at the 

      $(‘#nav a’).click(function(){

      Specifically, look at the 
      $container.attr(‘data-current’,selector);
      Code inside it.  Just add
      if(location.hash!=”"){        var hashfilter = “.” + location.hash.substr(1);    }    else{        var hashfilter = “*”;    }
      selector = hashfilter;

      }

      Right before it.
      Make sure you have all the functions from the “hash history” tutorial; the hashchange function, the code that detects the hash when the page loads, and so on.  

      The “hashchange” function from the “hash history” tutorial should take care of the rest.  Really, just copy-paste the code from both tutorials into one big JS file, and make sure that the “location.hash” is being plugged into “#container” “data-current attribute.  Delete the right code blocks and it should work fine.

      • Derick

        Thanks for the reply. I haven’t had any success following your instructions yet (I’m not very experienced with jquery) but I’m grateful for the fast response.

  • http://www.facebook.com/profile.php?id=100004749608632 Aline Rodrigues

    How use this in a external link?

  • Asd

    when your isotope container is located down the page and you want to prevent the whole page from jumping to the top, when a user klicks “all” (therefore #):

        var selector = $(this).attr(‘data-filter’); 
        var prettyselector = selector.substr(1); 
                                                       
        if(prettyselector==”"){
            location.hash = “none”;
        } else {
            location.hash = prettyselector;

    AND

        if(location.hash!=”#none”){
            var hashfilter = “.” + location.hash.substr(1);
        }
        else {
            var hashfilter = “*”;
        }

    no hands need to be layed on document.ready.

  • Bob Rast

    Great tutorials! I want to use three instances of isotope – each in a separate tab – to display lists of congressional and regulatory actions. I’m not well versed in javascript and I find that my first-panel isotope instance of isotope displays and sorts just fine, but in tabs 2 and 3 the display is collapsed into a pile. Is there a way to initiate multiple isotope instances on a page, and if so, would appreciate guidance on how to do it.

    Thanks,

    • Alexandre Smirnov

      How exactly are you initiating isotope for each one? Separate function calls or are you using only one selector, e.g. “$(‘.class1, class2′).isotope”?

  • http://www.matteochiti.it/ Matteo Chiti

    Hi and thanks for these tutorials about isotope! :)

    Can I apply this script together with the tutorial of active links and apply the hash to the title attribute?
    Thanks again! :)