Its been a while since I wrote my last tutorial in 2007 ” play youtube videos in flash tutorial “?and now im back with a different topic? jQuery *tsk tsk* now let?s get started!
What we are trying to?achieve?is a filterable portfolio based on its categories ( all, web & print ) using quicksand and jQuery…
Demo | Download
Download required files first:
- jQuery 1.3+
- QuickSand Plugin
- jQuery easing ( yes Robert Penners ?)
- Rotation and Scaling Plugin
The HTML:
i omitted the head section with the includes, you can find it in the source code downloadable above!
[cc lang=”html” tab_size=”2″ lines=”60″]
[/cc]
A closer look at the filter menu:
[cc lang=”html” tab_size=”2″ lines=”2″]
[/cc]
Notice that i added a title attribute for each, in our case here the title attribute is “web”, which will be pulled by jQuery later in a conditional statement…
A closer look to the list items of the portfolio:
[cc lang=”html” tab_size=”2″ lines=”2″]
[/cc]
notes the [data-id=”id-1″] which is used for sorting and the [data-type=”web”] used for filtering
The CSS:
Nothing fancy below just few lines of basic CSS styling
[cc lang=”css” tab_size=”2″ lines=”10″]
#content {width:960px; border: 1px solid #ddd; margin-left: auto ; margin-right: auto ; overflow:hidden; height:auto;}
#content ul { float:left;}
#content li { float:left; padding:3px; list-style:none; overflow:hidden;}
.box { width: 950px;}
[/cc]
And finally The jQuery “sortable.js”:
[cc lang=”jquery” tab_size=”2″ lines=”60″]
$(document).ready(function() {
// Custom sorting plugin // — No Need to edit this —\\
(function($) {
$.fn.sorted = function(customOptions) {
var options = {
reversed: false,
by: function(a) { return a.text(); }
};
$.extend(options, customOptions);
$data = $(this);
arr = $data.get();
arr.sort(function(a, b) {
var valA = options.by($(a));
var valB = options.by($(b));
if (options.reversed) {
return (valA < valB) ? 1 : (valA > valB) ? -1 : 0;
} else {
return (valA < valB) ? -1 : (valA > valB) ? 1 : 0;
}
});
return $(arr);
};
})(jQuery);
//—————————————- EDIT BELOW ——————————\\
// Filter Handler
var $filter = $(‘#menu a’);
// define portfolio “#div” with the portfolio list ( li ) elements in
var $portfolio = $(‘#content #items’);
// clone the “#div”
var $data = $portfolio.clone();
// attempt to call Quicksand on every click event handler
$filter.click(function(e) {
//check if the title attr is set to “all”
if ($($(this)).attr(“title”) == ‘all’) {
//if all then show all list ( li )
var $filteredData = $data.find(‘li’);
} else {
//get the title attribute dynamically using $(this)
var $filteredData = $data.find(‘li[data-type=’ + $($(this)).attr(“title”) + ‘]’);
}
// finally, call quicksand and dump the $fileredData in
$portfolio.quicksand($filteredData, {
duration: 800,
easing: ‘easeInOutQuad’
});
});
});
[/cc]
A closer look at the jQuery code :
the code below defines $filter, thats where the filter requests will come from based on clicks, now remember that each link element has a unique title attribute.
[cc lang=”jquery” tab_size=”2″ lines=”5″]
var $filter = $(‘#menu a’);
[/cc]
This one defines the path to the portfolio list and adds it to $portfolio
[cc lang=”jquery” tab_size=”2″ lines=”5″]
var $portfolio = $(‘#content #items’);
[/cc]
Clone the portfolio list
[cc lang=”jquery” tab_size=”2″ lines=”5″]
var $data = $portfolio.clone();
[/cc]
Add/bind a click event to the function where it first checks if the button/link which was clicked has the “all” attribute, if its true then it will locate all list elements and adds it to $filteredData, if it was false then it will grab the title attribute dynamically, it will then find lists with the similar title attributes and adds it to the $filteredData .
[cc lang=”jquery” tab_size=”2″ lines=”6″]
$filter.click(function(e) {
if ($($(this)).attr(“title”) == ‘all’) {
var $filteredData = $data.find(‘li’);
} else {
var $filteredData = $data.find(‘li[data-type=’ + $($(this)).attr(“title”) + ‘]’);
}
[/cc]
time for some magic *tada* call the quicksand and pass it the $filteredData along with Duration & easing type.
[cc lang=”jquery” tab_size=”2″ lines=”5″]
$portfolio.quicksand($filteredData, {
duration: 800,
easing: ‘easeInOutQuad’
});
[/cc]
Happy filtering ๐
nice job bro. Can’t wait to use this in my next project.
Glad you like it ya buZain, i deserve a 25% share of your next project ๐
Nice Job!
Works in IE?
Thanks
two words … screw IE ๐
I agree! ๐
I’m a total noob when it comes to web. I recently setup up a website from a template I got….and it has this setup. I have a gallery of videos with different tags. When I go to my gallery page it currently displays all my videos. Is there a way to set it to display a different tag instead of all?
Thanks heaps for writing this tutorial! Helped me out a bunch! ๐
I was just wondering if I would be able to get a little help with making a callback for the quicksand plugin? I’m a complete n00b when it comes to jquery and I’m having trouble figuring it out. I would be so veeeery very grateful if you could help me out! D: I’m using a simple jquery caption (http://thirdroute.com/projects/captify/) for my thumbnail images.
Hi there, great stuff. Is it possible to associate several data types to a single item as I’m looking to have portfolio items show up several different filters?
Just found this quick fix for multiple data types (thanks to: rozzy2049)
Original post at: https:\/\/zainals.com//zainals.com\/\/zainals.com//zainals.com//github.com/razorjack/quicksand/issues/21
– – – – – – –
Just in case anyone stumbles onto this issue. Look for the code in your main.js ( although I found it in the sortable.js ):
else {
// find all li elements that have our required $filterType
// values for the data-type element
var $filteredData = $data.find(‘li[data-type=’ + $filterType + ‘]’);
}
edit the line: var $filteredData = $data.find(‘li[data-type=’ + $filterType + ‘]’); to include ” ~ ” in the data type, so that the line should then read:
var $filteredData = $data.find(‘li[data-type~=’ + $filterType + ‘]’);
Once completed, you can go back to your itemized list and define multiple categories for a single data element, all you need to do is separate categories with a single blank space (i.e.):
Test
where “web” and “media” are two different categories that this data element will be sorted into…
Took me forever to solve this issue for myself. Hopefully someone else finds it handy
Thanks for the multiple data-types fix. Absolute life saver!
Thanks, your tutorial and example files helped a lot! ๐
Since the last post, the plugin have changed and i’m not able to post an image in multiple categories.
here’s my code
// check filter sort type
if(sorttype == “all”) {
var filterselect = pclone.find(“li”);
} else {
var filterselect = pclone.find(“li[class=”+sorttype+”]”);
}
plz help !
It’s been over 2 years since I got my hands on code, your code looks ok.
I know this might sound odd try replacing ” with ‘
Look back at the tut up and the codes posted by the awesome people at the comment section.
Also revise your HTML attributes, use Firefox firebug plugin to track javascript errors.
the download button is taking me to your website basically nothing is downloading and i got to the jquery website and i was not sure which one to download. is it the compressed or uncompressed one please. i really this was a plugin to save the stress of all this codes its quite hard to do this for a beginner
After 1 day finally i got what i need. (y)
Its Too much Awesome. <3 Great Work.