Красивая jQuery галерея в стиле Apple
В этом уроке мы сделаем красивую галерею с помощью jQuery и CSS3.
HTML
Для нашей галереи не понадобится база данных или дополнительный PHP код. Таким образом её легко встроить в уже существующий сайт, достаточно изменить немного строк HTML.
Рассмотрим html разметку:
<div id="main"> <div id="gallery"> <div id="slides"> <div><img src="img/sample_slides/macbook.jpg" width="920" height="400" /></div> <div><img src="img/sample_slides/iphone.jpg" width="920" height="400" /></div> <div><img src="img/sample_slides/imac.jpg" width="920" height="400" /></div> </div> <div id="menu"> <ul> <li> </li><li><a href=""><img src="img/sample_slides/thumb_macbook.png" /></a></li><li><a href=""><img src="img/sample_slides/thumb_iphone.png" /></a></li><li><a href=""><img src="img/sample_slides/thumb_imac.png" /></a></li> </ul> </div> </div> </div>
Идея проста — у нас есть два основных div’а — «menu» содержит уменьшенные картинки, и «slides» содержит сами изображения. Для добавления нового изображения достаточно добавить элементы в оба div’а. Перейдем к CSS.
CSS
Рассмотрим код, который используется непосредственно в галерее, остальной код CSS вы найдете в исходниках.
body,h1,h2,h3,p,quote,small,form,input,ul,li,ol,label{
/* Page reset */
margin:0px;
padding:0px;
}
body{
/* Setting default text color, background and a font stack */
color:#444444;
font-size:13px;
background: #f2f2f2;
font-family:Arial, Helvetica, sans-serif;
}
/* Gallery styles */
#gallery{
/* CSS3 Box Shadow */
-moz-box-shadow:0 0 3px #AAAAAA;
-webkit-box-shadow:0 0 3px #AAAAAA;
box-shadow:0 0 3px #AAAAAA;
/* CSS3 Rounded Corners */
-moz-border-radius-bottomleft:4px;
-webkit-border-bottom-left-radius:4px;
border-bottom-left-radius:4px;
-moz-border-radius-bottomright:4px;
-webkit-border-bottom-right-radius:4px;
border-bottom-right-radius:4px;
border:1px solid white;
background:url(img/panel.jpg) repeat-x bottom center #ffffff;
/* The width of the gallery */
width:920px;
overflow:hidden;
}
#slides{
/* This is the slide area */
height:400px;
/* jQuery изменит ширину на сумму всех слайдов. */
width:920px;
overflow:hidden;
}
.slide{
float:left;
}
#menu{
/* содержит маленькие картинки */
height:45px;
}
ul{
margin:0px;
padding:0px;
}
li{
/* каждый эскиз - элемент li */
width:60px;
display:inline-block;
list-style:none;
height:45px;
overflow:hidden;
}
li.inact:hover{
/* подсвечивание при наведении */
background:url(img/pic_bg.png) repeat;
}
li.act,li.act:hover{
background:url(img/active_bg.png) no-repeat;
}
li.act a{
cursor:default;
}
.fbar{
width:2px;
background:url(img/divider.png) no-repeat right;
}
li a{
display:block;
background:url(img/divider.png) no-repeat right;
height:35px;
padding-top:10px;
}
a img{
border:none;
}
Мы использовали CSS3 свойства: box-shadow, для его использования необходимо задать отступ (0 0), размытие (3px) и цвет тени; border-radius — для закругления углов.
Перейдем к jQuery.
jQuery
$(document).ready(function(){
/* этот код выполняется после загрузки всей страницы */
var totWidth=0;
var positions = new Array();
$('#slides .slide').each(function(i){
/* пробегаем все слайды и суммируем их ширину */
positions[i]= totWidth;
totWidth += $(this).width();
if(!$(this).width())
{
alert("Please, fill in width & height for all your images!");
return false;
}
});
$('#slides').width(totWidth);
$('#menu ul li a').click(function(e){
/* после клика на эскиз */
$('li.menuItem').removeClass('act').addClass('inact');
$(this).parent().addClass('act');
var pos = $(this).parent().prevAll('.menuItem').length;
$('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);
e.preventDefault();
});
$('#menu ul li.menuItem:first').addClass('act').siblings().addClass('inact');
/* активируем первый слайд */
});
Наша галерея готова!







Нет комментариев