I am building a dynamic filtering navigation bar for use with the isotope items in my document. I have a few different categories in my nav, each with relative subcategories. I am trying to build a nav where the Main categories show on load.
On load I also populate the DOM with all of the elements in random order. When you click a category link it filters out the results, and organizes the relative elements. I also want to show the relative subcategories to the filtered section. Then if you select a different category, the existing subnav fades out, and the relative sub nav fades in.
All of my sorting and everything relative to isotope works great. Its the intelligent show/hide of the subnav I am having a hard time with.
My HTML:
<nav id="filters">
<div id="categories">
<h2>Select a Category:</h2>
<br>
<li><a href="#" data-filter="*">All Categories</a></li>
<li><a href="#" data-filter=".autos" data-category="autos">Autos</a></li>
<li><a href="#" data-filter=".lifestyle" data-category="lifestyle">Lifestyle</a></li>
<li><a href="#" data-filter=".people" data-category="people">People</a></li>
<li><a href="#" data-filter=".tech" data-category="tech">Tech</a></li>
<li><a href="#" data-filter=".trends" data-category="trends">Trends</a></li>
<br>
</div>
<div id="autos">
<h2>Autos:</h2>
<li><a href="#" data-filter=".autos" data-category="autos">Show All</a></li>
<li><a href="#" data-filter=".news" data-category="autos">News</a></li>
<li><a href="#" data-filter=".car-tech" data-category="autos">Car Tech</a></li>
<li><a href="#" data-filter=".fuel-economy-and-safety" data-category="autos">Fuel Economy & Safety</a></li>
<li><a href="#" data-filter=".buying-and-selling" data-category="autos">Buying & Selling</a></li>
<li><a href="#" data-filter=".autos, .everything-else" data-category="autos">Everything Else</a></li>
<br>
</div>
<div id="lifestyle">
<h2>Lifestyle:</h2>
<li><a href="#" data-filter=".lifestyle" data-category="lifestyle">Show All</a></li>
<li><a href="#" data-filter=".travel" data-category="lifestyle">Travel</a></li>
<li><a href="#" data-filter=".music" data-category="lifestyle">Music</a></li>
<li><a href="#" data-filter=".food" data-category="lifestyle">Food</a></li>
<li><a href="#" data-filter=".shopping" data-category="lifestyle">Shopping</a></li>
<li><a href="#" data-filter=".lifestyle, .everything-else" data-category="lifestyle">Everything Else</a></li>
<br>
</div>
<div id="people">
<h2>People:</h2>
<li><a href="#" data-filter=".people" data-category="people">Show All</a></li>
<li><a href="#" data-filter=".who-you-know" data-category="people">Who You Know</a></li>
<li><a href="#" data-filter=".who-you-should-know" data-category="people">Who You Should Know</a></li>
<li><a href="#" data-filter=".people, .everyone-else" data-category="people">Everyone Else</a></li>
<br>
</div>
<div id="tech">
<h2>Tech:</h2>
<li><a href="#" data-filter=".tech" data-category="tech">Show All</a></li>
<li><a href="#" data-filter=".business" data-category="tech">Business</a></li>
<li><a href="#" data-filter=".pleasure" data-category="tech">Pleasure</a></li>
<li><a href="#" data-filter=".tech, .everything-else" data-category="tech">Everything Else</a></li>
<br>
</div>
<div id="trends">
<h2>Trends:</h2>
<li><a href="#" data-filter=".trends" data-category="trends">Show All</a></li>
<li><a href="#" data-filter=".online" data-category="trends">Online</a></li>
<li><a href="#" data-filter=".offline" data-category="trends">Offline</a></li>
<li><a href="#" data-filter=".trends, .everything-else" data-category="trends">Everything Else</a></li>
</div>
</nav>
My jQuery:
$(document).ready(function() {
$('#filters > div:gt(0)').hide();
$('#filters a').click(function(){
var selector = $(this).attr('data-filter');
var category = $(this).attr('data-category');
var $subnav = $('#filters').find('#'+category);
if($subnav.is(":hidden")) {
$subnav.fadeIn();
}
if($subnav.is(":visible") && $subnav.attr("id") != category) {
}
$container.isotope({ filter: selector });
return false;
});
});
I am open to any suggestions. Whether it is restructuring my nav, or redaclaring my variables in a new fashion. I have been staring at this block of code for a while, and brain is turning to mush. I was trying to use the second if statement to handle the hiding of the non-relative subnav.
If you like you can hop over to my site with the example on display:
Well, I figured it out. No changes to the HTML. Heres my updated JavaScript/jQuery