In this tutorial we will be making a portfolio with HTML5, jQuery and CSS3 that features an interesting growing effect.
The HTML
As usual, we start off with a blank HTML5 document, and add the needed stylesheets, markup and JavaScript includes.
04 |         <meta charset="utf-8" /> | 
05 |         <title>Growing Thumbnails Portfolio with jQuery & CSS3 | Tutorialzine Demo</title> | 
08 |         <link rel="stylesheet" href="assets/css/styles.css" /> | 
22 |             <h1>Dan's Portfolio</h1> | 
27 |             <h3>My Latest Projects</h3> | 
29 |             <a class="arrow prev">Prev</a> | 
30 |             <a class="arrow next">Next</a> | 
33 |                 <li class="visible"><a href=""><img src="assets/img/sites/s1.jpg" alt="" /></a></li> | 
41 |         <script src="assets/js/script.js"></script> | 
The important bit to note here is the 
#carousel  unordered list. This element holds a collection of li items that  represent your recent works.  The visible class is only added if the  thumbnail is to be shown. Only three thumbnails will be visible at a  time. The href attribute of the hyperlink can point to the website in  question, or a larger version of the image if you plan to use a lightbox  together with this example.

Growing Thumbnail Portfolio
The JavaScript
 
All of the JavaScript/jQuery code for this example resides in 
assets/js/script.js. We will write a JavaScript class called 
Navigator  that will manage the carousel for us. This will involve writing methods  for listening for clicks on the arrows, partitioning the carousel into  groups of 3 items and showing them.
Here is how the class will be used:
01 | $(document).ready(function(){ | 
04 |     var navigator = new Navigator({ | 
05 |         carousel: '#carousel', | 
06 |         nextButton: '.arrow.next', | 
07 |         prevButton: '.arrow.prev', | 
When the document is loaded, we will create an instance of the class,  passing the carousel div, the arrows and an optional parameter for  whether you want the list to be shuffled. There is one more parameter  that can go here – 
chunkSize. This property determines how many thumbnails will be featured at once, the default is 3.
The first step to achieve this, is to write the layout of the class:
02 | function Navigator(config) { | 
04 |     this.carousel = $(config.carousel);  | 
05 |     this.nextButton = $(config.nextButton);  | 
06 |     this.prevButton = $(config.prevButton);  | 
07 |     this.chunkSize = config.chunkSize || 3;  | 
08 |     this.shuffle = config.shuffle || false;  | 
11 |     this._items = $(config.carousel + ' li');  | 
13 |     this._visibleChunkIndex = 0;  | 
15 |     this.init = function () { | 
23 |     this.handlePrevClick = function(e) {}; | 
24 |     this.handleNextClick = function(e) {}; | 
27 |     this.showNextItems = function() {}; | 
30 |     this.showPrevItems = function() {}; | 
34 |     this._checkForBeginning = function() {}; | 
35 |     this._checkForEnd = function() {}; | 
39 |     this._splitItems = function(items, chunk) {}; | 
We are using an underscore to denote which properties and methods are  private. External code should not use any property that starts with an  underscore.
In the fragments below you can see how each of the methods are  implemented. First comes init(), which sets up the carousel by  binding  event listeners and partitioning the carousel ul.
01 | this.init = function () { | 
06 |         this._items.removeClass('visible'); | 
09 |         this._items.sort(function() { return 0.5 - Math.random() }); | 
12 |         this._items.slice(0, this.chunkSize).addClass('visible'); | 
16 |     this._chunks = this._splitItems(this._items, this.chunkSize); | 
21 |     self.nextButton.on('click', function(e) { | 
22 |         self.handleNextClick(e); | 
25 |     self.prevButton.on('click', function(e) { | 
26 |         self.handlePrevClick(e); | 
30 |     self.carousel.addClass('active'); | 
Next are the methods for handling arrow clicks.
01 | this.handlePrevClick = function (e) { | 
06 |     if (this._chunks[this._visibleChunkIndex - 1] !== undefined) { | 
11 | this.handleNextClick = function(e) { | 
16 |     if (this._chunks[this._visibleChunkIndex + 1] !== undefined) { | 
They call showPrevItems and showNextItems respectfully:
01 | this.showNextItems = function() { | 
04 |     $(this._chunks[this._visibleChunkIndex]).removeClass('visible'); | 
07 |     $(this._chunks[this._visibleChunkIndex + 1]).addClass('visible'); | 
10 |     this._visibleChunkIndex++; | 
17 | this.showPrevItems = function() { | 
20 |     $(this._chunks[this._visibleChunkIndex]).removeClass('visible'); | 
23 |     $(this._chunks[this._visibleChunkIndex - 1]).addClass('visible'); | 
26 |     this._visibleChunkIndex--; | 
29 |     this._checkForBeginning(); | 
The above methods remove or assign the 
visible  class, which is how we control the visibility of the thumbnails. It is a  good idea to hide the previous/next arrow if there are no further items  to show. This is done with the 
checkForBeginning and 
checkForEnd methods.
01 | this._checkForBeginning = function() { | 
02 |     this.nextButton.show();  | 
04 |     if (this._chunks[this._visibleChunkIndex - 1] === undefined) { | 
05 |         this.prevButton.hide(); | 
08 |         this.prevButton.show(); | 
12 | this._checkForEnd = function() { | 
13 |     this.prevButton.show();  | 
15 |     if (this._chunks[this._visibleChunkIndex + 1] === undefined) { | 
16 |         this.nextButton.hide(); | 
19 |         this.nextButton.show(); | 
Lastly, here is the 
splitItems method, which generates the  chunks. It relies on the splice JavaScript method for removing parts of  the array and adding them to the splitItems array (it becomes an array  of arrays):
01 | this._splitItems = function(items, chunk) { | 
06 |     while (items.length > 0) { | 
07 |         splitItems[i] = items.splice(0, chunk); | 
Congrats! You now have a working example. We are only left with styling it.

Growing CSS3 Effect
The CSS
 
The styling of the portfolio is defined in assets/css/styles.css.  Only the more interesting parts are shown here, as the rest is omitted  for brevity.
05 |     background-color:#111; | 
06 |     box-shadow:0 3px 5px #111; | 
12 |     -moz-transition:0.4s opacity; | 
13 |     -webkit-transition:0.4s opacity; | 
14 |     transition:0.4s opacity; | 
31 |     -moz-transition:0.4s all; | 
32 |     -webkit-transition:0.4s all; | 
61 |     background:url('../img/cradle.png') no-repeat top center; | 
62 |     background-size:contain; | 
 With this our Growing Thumbnails Portfolio is complete!
 
nice tutorials for web designer, i really like it,
ReplyDeletenice tutorial, thanks for sharing
ReplyDelete