n today’s tutorial we will be making a beautiful HTML5 portfolio powered by jQuery and the Quicksand plugin. You can use it to showcase your latest work and it is fully customizable, so potentially you could expand it to do much more.
The HTML
The first step is to write down the markup of a new HTML5 document.
In the head section, we will include the stylesheet for the page. The jQuery library, the Quicksand plugin and our script.js will go right before the closing body tag:
index.html
04 | <meta charset="utf-8" /> |
06 | <title>Making a Beautiful HTML5 Portfolio | Tutorialzine Demotitle> |
09 | <link rel="stylesheet" href="assets/css/styles.css" /> |
27 | <section id="container"> |
39 | <script src="assets/js/jquery.quicksand.js">script> |
40 | <script src="assets/js/script.js">script> |
In the body, there are a number of the new HTML5 elements. The header holds our h1 heading (which is styled as the logo), the section element holds the unordered list with the portfolio items (more lists are added by jQuery, as you will see later), and the nav element, styled as a green bar, acts as a content filter.
The #stage unordered list holds our portfolio items. You can see what these items should look like below. Each of them has a HTML5 data
attribute, which defines a series of comma-separated tags. Later, when
we use jQuery to loop through this list, we will record the tags and
create categories that can be selected from the green bar.
01 | <li data-tags="Print Design"> |
02 | <img src="assets/img/shots/1.jpg" /> |
05 | <li data-tags="Logo Design,Print Design"> |
06 | <img src="assets/img/shots/2.jpg" /> |
09 | <li data-tags="Web Design,Logo Design"> |
10 | <img src="assets/img/shots/3.jpg" /> |
You can put whatever you want in these li items and customize the
portfolio further. The Quicksand plugin will handle the animated
transitions of this list, so you are free to experiment.
Beautiful HTML5 Portfolio with jQuery
The jQuery
What the Quicksand plugin does, is compare two unordered lists of
items, find the matching LIs inside them, and animate them to their new
positions. The jQuery script we will be writing in this section, will
loop through the portfolio items in the #stage list, and
create a new (hidden) unordered list for each of the tags it finds. It
will also create a new menu option, which will trigger the quicksand
transition between the two lists.
First we need to listen for the ready event (the
earliest point in the loading of the page where we can access the DOM),
and loop through all the li items detecting the associated tags.
script.js – Part 1
01 | $(document).ready(function(){ |
03 | var items = $('#stage li'), |
08 | items.each(function(i){ |
10 | tags = elem.data('tags').split(','); |
13 | elem.attr('data-id',i); |
15 | $.each(tags,function(key,value){ |
18 | value = $.trim(value); |
20 | if(!(value in itemsByTags)){ |
22 | itemsByTags[value] = []; |
26 | itemsByTags[value].push(elem); |
Each tag is added to the itemsByTags object as an array. This would mean that itemsByTags['Web Design']
would hold an array with all the items that have Web Design as one of
their tags. We will use this object to create hidden unordered lists on
the page for quicksand.
It would be best to create a helper function that will handle it for us:
script.js – Part 2
01 | function createList(text,items){ |
09 | $.each(items,function(){ |
13 | $(this).clone().appendTo(ul); |
16 | ul.appendTo('#container'); |
25 | }).appendTo('#filter'); |
This function takes the name of the group and an array with LI items
as parameters. It then clones these items into a new UL and adds a link
in the green bar.
Now we have to loop through all the groups and call the above function, and also listen for clicks on the menu items.
script.js – Part 3
02 | createList('Everything',items); |
05 | $.each(itemsByTags,function(k,v){ |
09 | $('#filter a').live('click',function(e){ |
12 | link.addClass('active').siblings().removeClass('active'); |
17 | $('#stage').quicksand(link.data('list').find('li')); |
22 | $('#filter a:first').click(); |
Great! Now that we have everything in place we can move on to styling the page.
The CSS
Styling the page itself is not that interesting (you can see the CSS for this in assets/css/styles.css). However what is more interesting is the green bar (or the #filter bar), which uses the :before / :after
pseudo elements to add attractive corners on the sides of the bar. As
these are positioned absolutely, they also grow together with the bar.
styles.css
02 | background: url("../img/bar.png") repeat-x 0 -94px; |
10 | -moz-box-shadow:0 4px 4px #000; |
11 | -webkit-box-shadow:0 4px 4px #000; |
12 | box-shadow:0 4px 4px #000; |
15 | #filter:before, #filter:after { |
16 | background: url("../img/bar.png") no-repeat; |
23 | -moz-box-shadow:0 2px 0 rgba(0,0,0,0.4); |
24 | -webkit-box-shadow:0 2px 0 rgba(0,0,0,0.4); |
25 | box-shadow:0 2px 0 rgba(0,0,0,0.4); |
29 | background-position: 0 -47px; |
34 | background-position: 0 0; |
40 | display: inline-block; |
44 | text-shadow:1px 1px 1px #315218; |
52 | background: url("../img/bar.png") repeat-x 0 -138px; |
53 | box-shadow: 1px 0 0 rgba(255, 255, 255, 0.2), |
54 | -1px 0 0 rgba(255, 255, 255, 0.2), |
55 | 1px 0 1px rgba(0,0,0,0.2) inset, |
56 | -1px 0 1px rgba(0,0,0,0.2) inset; |
Before / After Elements
With this our beautiful HTML5 portfolio is complete!
Conclusion
You can use this template to present your work. The best thing about
it is that it’s really easy to customize. You only need to change the
contents of the initial LI items of the #stage list, and specify some
new tags – the script will do the rest. If the visitor doesn’t have
JavaScript enabled, they will still see all your portfolio items, which
is also good for SEO purposes.
0 komentar:
Posting Komentar