Magazine Architettura e Design

Come costruire un menu con immagini animate in jQuery

Da Iwebdesigner @Iwebdesigner_it

menu-con-sfondi-animati-in-jquery

Oggi vediamo come costruire un menu di navigazione con immagini di sfondo belle e animate da jQuery. L’immagine di sfondo di ogni voce del menu’ sara’ animata e fara’ la sua entrata scivolando in tempi diversi e con effetto veramente bello. L’immagine scivolera’ a seconda di dove arriva l’utente: se viene da destra, l’immagine scorrera’ da sinistra e viceversa.

Questo tutorial funziona su Chrome, Firefox, Opera, Safari, IE8, IE7 e IE6.

Le foto che abbiamo usato appartengono a B&W collezione su Flickr.

demo menu animato in jquery

Iniziamo a bomba!

IL CODICE HTML

L’ossatura e’ composta da un DIV wrapper e una lista non ordinata di voci del menu’.






Per far funzionare correttamente l’animazione dell’immagine di sfondo e’ necessario impostare la posizione della relativa immagine come stile inline. Come potete notare ogni voce del menu’ ha tre voci del sotto-menu’.

CODICE CSS

Iniziamo a vedere il foglio di stile dal menu’wrapper che contiene lo stile del font e le dimensioni degli elementi:

.menuWrapper{
    font-family: "Trebuchet MS", Arial, sans-serif;;
    font-size: 15px;
    font-style: normal;
    font-weight: normal;
    text-transform:uppercase;
    letter-spacing: normal;
    line-height: 1.45em;
    position:relative;
    margin:20px auto;
    height:542px;
    width:797px;
    background-position:0 0;
    background-repeat:no-repeat;
    background-color:transparent;
}

Il menu’ e le voci del menu’ seguiranno questo stile:

ul.menu{
    list-style:none;
    width:797px;
}
ul.menu > li{
    float:left;
    width:265px;
    height:542px;
    border-right:1px solid #777;
    background-repeat:no-repeat;
    background-color:transparent;
}
ul.menu > li.last{
    border:none;
}

Qui di seguito le tre classi che definiscono le immagini di background:

.bg1{
    background-image: url(../images/1.jpg);
}
.bg2{
    background-image: url(../images/2.jpg);
}
.bg3{
    background-image: url(../images/3.jpg);
}

Ed ecco il resto del nostro foglio di stile:

ul.menu > li > a{
    float:left;
    width:265px;
    height:50px;
    margin-top:450px;
    text-align:center;
    line-height:50px;
    color:#ddd;
    background-color:#333;
    letter-spacing:1px;
    cursor:pointer;
    text-decoration:none;
    text-shadow:0px 0px 1px #fff;
}
ul.menu > li ul{
    list-style:none;
    float:left;
    margin-top:-180px;
    width:100%;
    height:110px;
    padding-top:20px;
    background-repeat:no-repeat;
    background-color:transparent;
}
ul.menu > li ul li{
    display:none;
}
ul.menu > li ul.sub1{
    background-image:url(../images/bg1sub.png);
}
ul.menu > li ul.sub2{
    background-image:url(../images/bg2sub.png);
}
ul.menu > li ul.sub3{
    background-image:url(../images/bg3sub.png);
}
ul.menu > li ul li a{
    color:#fff;
    text-decoration:none;
    line-height:30px;
    margin-left:20px;
    text-shadow:1px 1px 1px #444;
    font-size:11px;
}

ul.menu > li ul li a:hover{
    border-bottom:1px dotted #fff;
}
ul.menu > li ul.sub1 li{
    display:block;
}

Il selettore “>” affronta solo gli elementi del menu’ principale e non applica gli effetti alle voci del sotto-menu’.

IL CODICE JAVASCRIPT

Ecco cosa avviene nello script: verifica da dove proviene l’utente e poi anima le immagini di fondo degli elementi del menu’ principale e il profilo del sotto-menu’ di conseguenza.

$(function() {
	/* position of the 
  • that is currently shown */ var current = 0; $('#bg1,#bg2,#bg3').mouseover(function(e){ var $this = $(this); /* if we hover the current one, then don't do anything */ if($this.parent().index() == current) return; /* item is bg1 or bg2 or bg3, depending where we are hovering */ var item = e.target.id; /* this is the sub menu overlay. Let's hide the current one if we hover the first
  • or if we come from the last one, then the overlay should move left -> right, otherwise right->left */ if(item == 'bg1' || current == 2) $('#menu .sub'+parseInt(current+1)) .stop() .animate({backgroundPosition:"(-266px 0)"},300,function(){ $(this).find('li').hide(); }); else $('#menu .sub'+parseInt(current+1)) .stop() .animate({backgroundPosition:"(266px 0)"},300,function(){ $(this).find('li').hide(); }); if(item == 'bg1' || current == 2){ /* if we hover the first
  • or if we come from the last one, then the images should move left -> right */ $('#menu > li') .animate({backgroundPosition:"(-800px 0)"},0) .removeClass('bg1 bg2 bg3') .addClass(item); move(1,item); } else{ /* if we hover the first
  • or if we come from the last one, then the images should move right -> left */ $('#menu > li') .animate({backgroundPosition:"(800px 0)"},0) .removeClass('bg1 bg2 bg3') .addClass(item); move(0,item); } /* We want that if we go from the first one to the last one (without hovering the middle one), or from the last one to the first one, the middle menu's overlay should also slide, either from left to right or right to left. */ if(current == 2 & item == 'bg1'){ $('#menu .sub'+parseInt(current)) .stop() .animate({backgroundPosition:"(-266px 0)"},300); } if(current == 0 & item == 'bg3'){ $('#menu .sub'+parseInt(current+2)) .stop() .animate({backgroundPosition:"(266px 0)"},300); } /* change the current element */ current = $this.parent().index(); /* let's make the overlay of the current one appear */ $('#menu .sub'+parseInt(current+1)) .stop().animate({backgroundPosition:"(0 0)"},300,function(){ $(this).find('li').fadeIn(); }); }); /* dir:1 - move left->right dir:0 - move right->left */ function move(dir,item){ if(dir){ $('#bg1').parent() .stop() .animate({backgroundPosition:"(0 0)"},200); $('#bg2').parent() .stop() .animate({backgroundPosition:"(-266px 0)"},300); $('#bg3').parent() .stop() .animate({backgroundPosition:"(-532px 0)"},400,function(){ $('#menuWrapper').removeClass('bg1 bg2 bg3') .addClass(item); }); } else{ $('#bg1').parent() .stop() .animate({backgroundPosition:"(0 0)"},400,function(){ $('#menuWrapper').removeClass('bg1 bg2 bg3') .addClass(item); }); $('#bg2').parent() .stop() .animate({backgroundPosition:"(-266px 0)"},300); $('#bg3').parent() .stop() .animate({backgroundPosition:"(-532px 0)"},200); } } });
  • download menu animato jquery

    Ecco fatto, questo e’ tutto e godetevi l’effetto.

    Alla prossima!


    Potrebbero interessarti anche :

    Ritornare alla prima pagina di Logo Paperblog

    Possono interessarti anche questi articoli :

    Dossier Paperblog