• RSS
  • Facebook
  • Twitter
Comments


Created by Martin Angelov on Mar 9th, 2010
Mosaic Slideshow
 
When designing a product page, it is often necessary to present a number of images in a succession, also known as a slideshow. With the raise of the jQuery library and its numerous plugins, there is an abundance of ready-made solutions which address this problem. However, to make a lasting impression to your visitors, you need to present them with something they have not seen before.
Today we are making a jQuery & CSS mosaic gallery. Mosaic, because it will feature an interesting tile transition effect when moving from one slide to another.

Step 1 – XHTML

The mosaic effect of the slideshow is achieved by dividing the original image into smaller parts. These tiles, which contain parts of the image, are sequentially hidden from view, which causes the effect. The markup of the slideshow is pretty straightforward. It consists of the main slideshow container element (#mosaic-slideshow), a left and right arrow for previous and next transition and the mosaic-slide div, which is inserted by jQuery at run-time.

demo.html

01<div id="mosaic-slideshow">
02    <div class="arrow left">div>
03    <div class="arrow right">div>
04 
05    <div class="mosaic-slide" style="z-index: 10;">
06 
07        
08        <div class="tile" style="...">div>
09        <div class="tile" style="...">div>
10        <div class="tile" style="...">div>
11        <div class="tile" style="...">div>
12 
13    div>
14div>
The div with the mosaic-slide class name is added to the page by jQuery after the transition() JavaScript function is executed (we will come back to this in the third step). Inside it you can see the tile divs. There are a total of 56 such divs, each of which has a 60px by 60px portion of the slide image set as its background.
Mosaic Slideshow
Mosaic Slideshow

Step 2 – CSS

To make this effect work (and most importantly look good), we have to add a few lines of CSS. Only the code directly used by the gallery is shown here. You can see the code that styles the rest of the demonstration page in styles.css.

styles.css – Part 1

01#mosaic-slideshow{
02    /* The slideshow container div */
03    height:500px;
04    margin:0 auto;
05    position:relative;
06    width:670px;
07}
08 
09.mosaic-slide{
10    /* This class is shared between all the slides */
11    left:80px;
12    position:absolute;
13    top:25px;
14 
15    border:10px solid #555;
16 
17    /* CSS3 rounded corners */
18    -moz-border-radius:20px;
19    -webkit-border-radius:20px;
20    border-radius:20px;
21}
22 
23.tile{
24    /* The individual tiles */
25    height:60px;
26    width:60px;
27    float:left;
28    border:1px solid #555;
29    border-width:0 1px 1px 0;
30    background-color:#555;
31}
The slideshow is contained inside the div with an ID of mosaic-slideshow (or #mosaic-slideshow, if we refer to it in a form of a CSS / jQuery selector).  There can be only one such div in the page, hence the use of an ID attribute.
However there can be more than one mosaic-slide divs in the page. The effect itself is achieved by stacking two slides on top of each other and hiding the tiles of the first one to reveal the ones of the second. This is why we are using a class name instead of an ID.
Some of the more interesting rules presented here are the three CSS3 rules for rounded corners. As the CSS3 standard is still a work in progress, browsers don’t support the regular border-radius property yet (except for the new 10.50 version of Opera), and need vendor-specific prefixes to recognize it. The -moz- prefix is used by Firefox, and -webkit- is used by Safari and Chrome.

styles.css – Part 2

01.arrow{
02    /* The prev/next arrows */
03    width:35px;
04    height:70px;
05    background:url("img/arrows.png") no-repeat;
06    position:absolute;
07    cursor:pointer;
08    top:50%;
09    margin-top:-35px;
10}
11 
12.arrow.left{
13    left:15px;
14    background-position:center top;
15}
16 
17.arrow.left:hover{
18    background-position:center -70px;
19}
20 
21.arrow.right{
22    right:15px;
23    background-position:center -140px;
24}
25 
26.arrow.right:hover{
27    background-position:center -210px;
28}
29 
30.clear{
31    /* This class clears the floats */
32    clear:both;
33}
The arrow class is shared by the previous and next arrows. They do need individual styling in addition to this common rule, so we add it after this. We are also using a CSS sprite as the background for the arrow divs. It contains a regular and hover state for both arrows, which spares us from having to use four individual images.
CSS spriting” is a widespread technique used by web designers. It allows the designer to join multiple smaller images into a single larger one, called a sprite, which is downloaded faster and saves the web server from multiple download requests. After this, the designer can use the CSS background property in conjunction with setting the elements to a fixed size, to show only the part of the sprite image that they need.
Mosaic Slideshow
Mosaic Slideshow

Step 3 – jQuery

After including the jQuery library to the page, we can move on to creating the script that will make the slideshow tick. To achieve the mosaic effect, the script defines 4 functions:
  • transition() – this function makes an animated transition between the currently shown slide, and a new one specified by the id parameter. It works by positioning the new slide we want to show, below the current one, and then hiding the current one one tile at a time;
  • generateGrid() – this function is used by transition() to generate a grid of tiles. Each tile contains a part of the slide image as its background;
  • next() – detects which the next slide is and runs the transition() function with its index;
  • prev() – analogous to next().

script.js – Part 1

01/* The slide images are contained in the slides array. */
02var slides = new Array('img/slide_1.jpg',
03                       'img/slide_2.jpg',
04                       'img/slide_3.jpg',
05                       'img/slide_4.jpg',
06                       'img/slide_5.jpg');
07 
08$(document).ready(function(){
09    /* This code is executed after the DOM has been completely loaded */
10 
11    $('.arrow.left').click(function(){
12        prev();
13 
14        /* Clearing the autoadvance if we click one of the arrows */
15        clearInterval(auto);
16    });
17 
18    $('.arrow.right').click(function(){
19        next();
20        clearInterval(auto);
21    });
22 
23    /* Preloading all the slide images: */
24 
25    for(var i=0;i
26    {
27        (new Image()).src=slides[i];
28    }
29 
30    /* Showing the first one on page load: */
31    transition(1);
32 
33    /* Setting auto-advance every 10 seconds */
34 
35    var auto;
36 
37    auto=setInterval(function(){
38        next();
39    },10*1000);
40});
The $(document).ready() method is executed once the page has finished loading. This will ensure that all the divs and other elements are accessible to the script. Inside it we bind a function for the click event on the previous and next arrows, preload all the images, show the first slide (otherwise the slideshow would be empty) and set up the auto-advance interval.

script.js – Part 2

01var current = {};
02function transition(id)
03{
04    /* This function shows the slide specified by the id. */
05 
06    if(!slides[id-1]) return false;
07 
08    if(current.id)
09    {
10        /* If the slide we want to show is currently shown: */
11        if(current.id == id) return false;
12 
13        /* Moving the current slide layer to the top: */
14        current.layer.css('z-index',10);
15 
16        /* Removing all other slide layers that are positioned below */
17        $('.mosaic-slide').not(current.layer).remove();
18    }
19 
20    /* Creating a new slide and filling it with generateGrid: */
21    var newLayer = $('
').html(generateGrid({rows:7,cols:8,image:slides[id-1]}));
22 
23    /* Moving it behind the current slide: */
24    newLayer.css('z-index',1);
25 
26    $('#mosaic-slideshow').append(newLayer);
27 
28    if(current.layer)
29    {
30        /* Hiding each tile of the current slide, exposing the new slide: */
31        $('.tile',current.layer).each(function(i){
32            var tile = $(this);
33            setTimeout(function(){
34                tile.css('visibility','hidden');
35            },i*10);
36        })
37    }
38 
39    /* Adding the current id and newLayer element to the current object: */
40    current.id = id;
41    current.layer = newLayer;
42}
The transition function uses the global current object to store the id of the currently shown slide, and a reference to the current slide div. This is later used to remove leftover slides and prevent a transition from occurring if the same slide as the currently active one is to be shown.
Notice how we use the each method on line 31 to loop through the tiles of the current slide and schedule them to be hidden in i*10 milliseconds in the future. As i is incremented for every tile, this mean that they are hidden 10 milliseconds apart from one another.
Slide Transition
Slide Transition

script.js – Part 3

01function next()
02{
03    if(current.id)
04    {
05        transition(current.id%slides.length+1);
06    }
07}
08 
09function prev()
10{
11    if(current.id)
12    {
13        transition((current.id+(slides.length-2))%slides.length+1);
14    }
15 
16}
17 
18/* Width and height of the tiles in pixels: */
19var tabwidth=60, tabheight=60;
20 
21function generateGrid(param)
22{
23    /* This function generates the tile grid, with each tile containing a part of the slide image */
24 
25    /* Creating an empty jQuery object: */
26    var elem = $([]),tmp;
27 
28    for(var i=0;i
29    {
30        for(var j=0;j
31        {
32            tmp = $('
', {
33                    "class":"tile",
34                    "css":{
35                        "background":'#555 url('+param.image+') no-repeat '+(-j*tabwidth)+'px '+(-i*tabheight)+'px'
36                    }
37            });
38 
39            /* Adding the tile to the jQuery object: */
40            elem = elem.add(tmp);
41        }
42 
43        /* Adding a clearing element at the end of each line. This will clearly divide the divs into rows: */
44        elem = elem.add('
');
45    }
46 
47    return elem;
48}
The parameter passed to generateGrid() is an object containing the rows and the columns we want to be generated, as well as the image to be set as the background of the tiles. While generating the tiles, the background image is offset according to the current position of the tile in the row and in the column. Finally the tile is added to an empty jQuery object which is returned at the end.
With this the mosaic slideshow is complete!

Wrapping it up

Today we created a slideshow with an animated mosaic transition effect. You can modify it to include a different number of rows and columns or change the way slides are changed entirely.


Postado por Fernando Schimit - Fox Creative
[...]

Comments

http://tutorialzine.com

Created by Martin Angelov on Mar 24th, 2010
Designing and coding a sponsors page is part of the developer’s life (at least the lucky developer’s life, if it is about a personal site of theirs). It, however, follows different rules than those for the other pages of the site. You have to find a way to fit a lot of information and organize it clearly, so that the emphasis is put on your sponsors, and not on other elements of your design.
We are using PHP, CSS and jQuery with the jQuery Flip plug-in, to do just that. The resulting code can be used to showcase your sponsors, clients or portfolio projects as well.

Step 1 – XHTML

Most of the markup is generated by PHP for each of the sponsors after looping the main $sponsor array. Below you can see the code that would be generated and outputted for Google:

demo.php

01<div title="Click to flip" class="sponsor">
02    <div class="sponsorFlip">
03        <img alt="More about google" src="img/sponsors/google.png">
04    div>
05
06    <div class="sponsorData">
07        <div class="sponsorDescription">
08            The company that redefined web search.
09        div>
10        <div class="sponsorURL">
11            <a href="http://www.google.com/">http://www.google.com/ a>
12        div>
13    div>
14div>
The outermost .sponsor div contains two additional div elements. The first – sponsorFlip – contains the company logo. Every click on this element causes the Flip effect to be initiated, as you will see in the jQuery part of the tutorial.
Maybe more interesting is the sponsorData div. It is hidden from view with a display:none CSS rule, but is accessible to jQuery. This way we can pass the description and the URL of the sponsoring company to the front end. After the flipping animation is complete, the contents of this div is dynamically inserted into sponsorFlip.
Sponsor Flip Wall
Sponsor Flip Wall

Step 2 – CSS

We can start laying down the styling of the wall, as without it there is no much use of the page. The code is divided in two parts. Some classes are omitted for clarity. You can see all the styles used by the demo in styles.css in the download archive.

styles.css – Part 1

01body{
02    /* Setting default text color, background and a font stack */
03    font-size:0.825em;
04    color:#666;
05    background-color:#fff;
06    font-family:Arial, Helvetica, sans-serif;
07}
08
09.sponsorListHolder{
10    margin-bottom:30px;
11}
12
13.sponsor{
14    width:180px;
15    height:180px;
16    float:left;
17    margin:4px;
18
19    /* Giving the sponsor div a relative positioning: */
20    position:relative;
21    cursor:pointer;
22}
23
24.sponsorFlip{
25    /*  The sponsor div will be positioned absolutely with respect
26        to its parent .sponsor div and fill it in entirely */
27
28    position:absolute;
29    left:0;
30    top:0;
31    width:100%;
32    height:100%;
33    border:1px solid #ddd;
34    background:url("img/background.jpg") no-repeat center center #f9f9f9;
35}
36
37.sponsorFlip:hover{
38    border:1px solid #999;
39
40    /* CSS3 inset shadow: */
41    -moz-box-shadow:0 0 30px #999 inset;
42    -webkit-box-shadow:0 0 30px #999 inset;
43    box-shadow:0 0 30px #999 inset;
44}
After styling the sponsor and sponsorFlip divs, we add a :hover state for the latter. We are using CSS3 inset box-shadow to mimic the inner shadow effect you may be familiar with from Photoshop. At the moment of writing inset shadows only work in the latest versions of Firefox, Opera and Chrome, but being primarily a visual enhancement, without it the page is still perfectly usable in all browsers.

styles.css – Part 2

01.sponsorFlip img{
02    /* Centering the logo image in the middle of the .sponsorFlip div */
03
04    position:absolute;
05    top:50%;
06    left:50%;
07    margin:-70px 0 0 -70px;
08}
09
10.sponsorData{
11    /* Hiding the .sponsorData div */
12    display:none;
13}
14
15.sponsorDescription{
16    font-size:11px;
17    padding:50px 10px 20px 20px;
18    font-style:italic;
19}
20
21.sponsorURL{
22    font-size:10px;
23    font-weight:bold;
24    padding-left:20px;
25}
26
27.clear{
28    /* This class clears the floats */
29    clear:both;
30}
As mentioned earlier, the sponsorData div is not meant for viewing, so it is hidden with display:none. Its purpose is to only store the data which is later extracted by jQuery and displayed at the end of the flipping animation.
Flipping Animation
Flipping Animation

Step 3 – PHP

You have many options in storing your sponsors data – in a MySQL database, XML document or even a plain text file. These all have their benefits and we’ve used all of them in the previous tutorials (except XML storage, note to self).
However, the sponsor data is not something that changes often. This is why a different approach is needed. For the purposes of the task at hand, we are using a multidimensional array with all the sponsor information inside it. It is easy to update and even easier to implement:

demo.php – Part 1

01// Each sponsor is an element of the $sponsors array:
02
03$sponsors = array(
04    array('facebook','The biggest social..','http://www.facebook.com/'),
05    array('adobe','The leading software de..','http://www.adobe.com/'),
06    array('microsoft','One of the top software c..','http://www.microsoft.com/'),
07    array('sony','A global multibillion electronics..','http://www.sony.com/'),
08    array('dell','One of the biggest computer develo..','http://www.dell.com/'),
09    array('ebay','The biggest online auction and..','http://www.ebay.com/'),
10    array('digg','One of the most popular web 2.0..','http://www.digg.com/'),
11    array('google','The company that redefined w..','http://www.google.com/'),
12    array('ea','The biggest computer game manufacturer.','http://www.ea.com/'),
13    array('mysql','The most popular open source dat..','http://www.mysql.com/'),
14    array('hp','One of the biggest computer manufacturers.','http://www.hp.com/'),
15    array('yahoo','The most popular network of so..','http://www.yahoo.com/'),
16    array('cisco','The biggest networking and co..','http://www.cisco.com/'),
17    array('vimeo','A popular video-centric social n..','http://www.vimeo.com/'),
18    array('canon','Imaging and optical technology ma..','http://www.canon.com/')
19);
20
21// Randomizing the order of sponsors:
22
23shuffle($sponsors);
The sponsors are grouped into the main $sponsors array. Each sponsor entry is organized as a separate regular array. The first element of that array is the unique key of the sponsor, which corresponds to the file name of the logo. The second element is a description of the sponsor and the last is a link to the sponsor’s website.
After defining the array, we use the in-build shuffle() PHP function to randomize the order in which the sponsors are displayed.

demo.php – Part 2

01// Looping through the array:
02
03foreach($sponsors as $company)
04{
05    echo'
06        
class="sponsor" title="Click to flip">
07            
class="sponsorFlip">
08                "img/sponsors/'.$company[0].'.png" alt="More about '.$company[0].'" />
09            
10
11            
class="sponsorData">
12                
class="sponsorDescription">
13                    '.$company[1].'
14                
15                
class="sponsorURL">
16                    "'.$company[2].'">'.$company[2].'
17                
18            
19        
20
21    ';
22}
The code above can be found halfway down demo.php. It basically loops through the shuffled $sponsors array and outputs the markup we discussed in step one. Notice how the different elements of the array are inserted into the template.

Step 4 – jQuery

The jQuery Flip plugin requires both the jQuery library and jQuery UI. So, after including those in the page, we can move on with writing the code that will bring our sponsor wall to life.

script.js

01$(document).ready(function(){
02    /* The following code is executed once the DOM is loaded */
03
04    $('.sponsorFlip').bind("click",function(){
05
06        // $(this) point to the clicked .sponsorFlip element (caching it in elem for speed):
07
08        var elem = $(this);
09
10        // data('flipped') is a flag we set when we flip the element:
11
12        if(elem.data('flipped'))
13        {
14            // If the element has already been flipped, use the revertFlip method
15            // defined by the plug-in to revert to the default state automatically:
16
17            elem.revertFlip();
18
19            // Unsetting the flag:
20            elem.data('flipped',false)
21        }
22        else
23        {
24            // Using the flip method defined by the plugin:
25
26            elem.flip({
27                direction:'lr',
28                speed: 350,
29                onBefore: function(){
30                    // Insert the contents of the .sponsorData div (hidden
31                    // from view with display:none) into the clicked
32                    // .sponsorFlip div before the flipping animation starts:
33
34                    elem.html(elem.siblings('.sponsorData').html());
35                }
36            });
37
38            // Setting the flag:
39            elem.data('flipped',true);
40        }
41    });
42
43});
First we bind a function as a listener for the click event on the .sponsorFlip divs.  After a click event occurs, we check whether the flipped flag is set via the jquery data() method. This flag is set individually for each sponsorFlip div and helps us determine whether the div has already been flipped. If this is so, we use the revertFlip() method which is defined by the Flip plugin. It returns the div to its previous state.
If the flag is not present, however, we initiate a flip on the element. As mentioned earlier, the .sponsorData div, which is contained in every sponsor div, contains the description and the URL of the sponsor, and is hidden from view with CSS. Before the flipping starts, the plug-in executes the onBefore function we define in the configuration object that is passed as a parameter (line 29). In it we change the content of the sponsorFlip div to the one of sponsorData div, which replaces the logo image with information about the sponsor.
With this our sponsor flip wall is complete!

Conclusion

Today we used the jQuery Flip plug-in to build a sponsor wall for your site. You can use this example to bring interactivity to your site’s pages. And as the data for the wall is read from an array, you can easily modify it to work with any kind of database or storage.


Postado por Fernando Schimit - Fox Creative
[...]