Nice CSS3 Lightbox gallery With jQuery. This tutorial will about creating nice looking photo gallery where we will using and CSS3 and jQuery too (with combination with drag and drop and fancybox plugin). Our gallery will some pane, where we will able to see scattered photos. We will able to drag and drop these photos (like real photos at your table), and also open each photo separately (via clicking at it). I think that this will interesting and you will like it.
Here are links to demo and downloadable package:
[sociallocker]
[/sociallocker]
Now please download the example files and lets start coding !
Step 1. HTML
index.html
As usual, we start with the HTML. I don`t will scare you with big code here, but will split this into logic sections. First is header area:
04 | <title>Nice CSS3 Lightbox Gallery With jQuery | Script tutorials</title> |
05 | <link rel="stylesheet" type="text/css" href="css/style.css" /> |
06 | <link rel="stylesheet" type="text/css" href="fancybox/jquery.fancybox-1.2.6.css" /> |
08 | <script type="text/javascript" src="js/jquery-css-transform.js"></script> |
09 | <script type="text/javascript" src="js/jquery-animate-css-rotate-scale.js"></script> |
11 | <script type="text/javascript" src="fancybox/jquery.fancybox-1.2.6.pack.js"></script> |
12 | <script type="text/javascript" src="js/main.js"></script> |
Here I linking 2 CSS files and 6 JS files. This is for fancybox library, jQuery library, 2 files (jquery-css-transform.js and jquery-animate-css-rotate-scale.js) will allow us to perform rotate animation, and – few files – our own customization. Last part is gallery itself (parent object with images inside):
02 | <div id="1" style="background-image:url(images/thumb_pic1.jpg)"> |
03 | <a class="fancybox" href="images/pic1.jpg" target="_blank">photo 1</a> |
05 | <div id="2" style="background-image:url(images/thumb_pic2.jpg)"> |
06 | <a class="fancybox" href="images/pic2.jpg" target="_blank">photo 2</a> |
08 | <div id="3" style="background-image:url(images/thumb_pic3.jpg)"> |
09 | <a class="fancybox" href="images/pic3.jpg" target="_blank">photo 3</a> |
11 | <div id="4" style="background-image:url(images/thumb_pic4.jpg)"> |
12 | <a class="fancybox" href="images/pic4.jpg" target="_blank">photo 4</a> |
14 | <div id="5" style="background-image:url(images/thumb_pic5.jpg)"> |
15 | <a class="fancybox" href="images/pic5.jpg" target="_blank">photo 5</a> |
17 | <div id="6" style="background-image:url(images/thumb_pic6.jpg)"> |
18 | <a class="fancybox" href="images/pic6.jpg" target="_blank">photo 6</a> |
20 | <div id="7" style="background-image:url(images/thumb_pic7.jpg)"> |
21 | <a class="fancybox" href="images/pic7.jpg" target="_blank">photo 7</a> |
23 | <div id="8" style="background-image:url(images/thumb_pic8.jpg)"> |
24 | <a class="fancybox" href="images/pic8.jpg" target="_blank">photo 8</a> |
26 | <div id="9" style="background-image:url(images/thumb_pic9.jpg)"> |
27 | <a class="fancybox" href="images/pic9.jpg" target="_blank">photo 9</a> |
29 | <div id="10" style="background-image:url(images/thumb_pic10.jpg)"> |
30 | <a class="fancybox" href="images/pic10.jpg" target="_blank">photo 10</a> |
32 | <div id="11" style="background-image:url(images/thumb_pic11.jpg)"> |
33 | <a class="fancybox" href="images/pic11.jpg" target="_blank">photo 11</a> |
35 | <div id="12" style="background-image:url(images/thumb_pic12.jpg)"> |
36 | <a class="fancybox" href="images/pic12.jpg" target="_blank">photo 12</a> |
Here we can see 12 added objects – elements of gallery. Hope all clean here.
Step 2. CSS
Here are our styles:
css/style.css
08 | background-color:#fff; |
11 | border:1px #000 solid; |
15 | -moz-border-radius:3px; |
16 | -webkit-border-radius:3px; |
23 | #gallery div,#gallery div a { |
29 | background-color:#fff; |
30 | background-position:50% 50%; |
31 | background-repeat:no-repeat; |
32 | border:2px solid #000; |
38 | -moz-border-radius:3px; |
39 | -webkit-border-radius:3px; |
40 | -moz-box-shadow:3px 3px 4px #444; |
41 | -webkit-box-shadow:3px 3px 4px #444; |
42 | box-shadow:3px 3px 4px #444; |
Step 3. JS (jQuery)
Now, its time to understand how we initializing our gallery:
js/main.js
01 | $(document).ready(function(){ |
03 | $('#gallery div').each(function() { |
04 | var iRL = Math.floor(Math.random() * 500); |
05 | var iRT = Math.floor(Math.random() * 350); |
06 | var iMT = Math.floor(Math.random() * 100 - 50); |
13 | var bPrevClick = false; |
14 | $('#gallery div').draggable({ |
15 | containment: 'parent', |
16 | start: function(e,ui) { |
19 | stop: function(e, ui) { |
20 | setTimeout(function() { |
25 | $('#gallery div a').bind('click',function(e) { |
28 | e.stopImmediatePropagation(); |
31 | $('#gallery div').mousedown(function(e) { |
33 | $('#gallery div').each(function() { |
34 | var z = parseInt($(this).css('zIndex')) |
35 | iMZ = (z > iMZ) ? z : iMZ; |
37 | $(e.target).closest('#gallery div').css({ |
41 | $('a.fancybox').fancybox({ |
From beginning – I rendomize positions of our photos – random positions and rotate degreee. Then – I had a task to prevent lightbox from loading full-photo during moving (drag and drop) photo. And in the end – fancybox initialization.
Step 4. Images
All our images located in ‘images’ folder. Here are thumb images (thumb_pic1.jpg, thumb_pic2.jpg .. thumb_pic12.jpg) and full-size images too(pic1.jpg, pic2.jpg .. pic12.jpg)
Conclusion
New great photo gallery is done! I hope that you enjoyed this article. Good luck in your projects!