Script Tutorials

<!--Tutorials for Web Developers -->
  • Rss Feed
  • Email Updates
  • Script Tutorials on Twitter
  • Script Tutorials on Facebook
  • Script Tutorials on Google+
  • Script Tutorials on LinkedIn
  • HTML/CSS
    • Game Development →
    • Menus →
    • Templates →
    • CSS3 tutorials →
  • HTML5
  • jQuery
  • PHP
  • JS
  • Res
    • Infographic →
    • jQuery plugin roundups →
    • HTML5 stuff →
    • Mobile →
    • Other →
  • XSLT
  • Deals
Manuals

How to make a 3D gallery using javascript

Date: 22nd Apr 2011 Author: admin 12 Comments
Posted in: JavaScript |Tags: 3d, gallery, how-to, javascript, tutorial

Tweet
3D photo gallery

3D gallery – using javascript

Today we continue JavaScript lessons, and our article will about creating modern 3d photo gallery using pure javascript. We will simulate 3D effect using z-indexes. Via mouse clicking we will moving from one photo to another. And I sure that better to see demo now.

Here are sample and downloadable package:

Live Demo
download in package

Ok, download the example files and lets start coding !


Step 1. HTML

As usual, we start with the HTML.

This is our main page code of 3d gallery.

index.html

<link rel="stylesheet" href="css/main.css" type="text/css" media="all" />
<script src="js/main.js" type="text/javascript"></script>

<div class="example" id="gall">
    <img src="images/pic1.jpg"><span>Picture 1 title<br>and description.</span>
    <img src="images/pic2.jpg"><span>Picture 2 description.</span>
    <img src="images/pic3.jpg"><span>Picture 3 description.</span>
    <img src="images/pic4.jpg"><span>Picture 4 description.</span>
    <img src="images/pic5.jpg"><span>Picture 5 description.</span>
    <img src="images/pic6.jpg"><span>Picture 6 description.</span>
    <img src="images/pic7.jpg"><span>Picture 7 description.</span>
    <img src="images/pic8.jpg"><span>Picture 8 description.</span>
    <img src="images/pic9.jpg"><span>Picture 9 description.</span>
    <img src="images/pic10.jpg"><span>Picture 10 description.</span>
    <img src="images/pic11.jpg"><span>Picture 11 description.</span>
    <img src="images/pic12.jpg"><span>Picture 12 description.</span>
    <img src="images/pic13.jpg"><span>Picture 13 description.</span>
    <img src="images/pic14.jpg"><span>Picture 14 description.</span>
    <img src="images/pic15.jpg"><span>Picture 15 description.</span>
</div>

As we can see – I put all used images for gallery in ‘images’ folder. All pretty easy here.

Step 2. CSS

Here are used CSS styles:

css/main.css

body{background:#333;margin:0;padding:0}

.example {
    position:absolute;
    left: 0%;
    top: 0%;
    width: 100%;
    height: 95%;
    background: #333;
    overflow: hidden;
}
.example img {
    position: absolute;
    background: #666;
    overflow: hidden;
    cursor: pointer;
    left: 100%;
    border-color: #333;
    border-style: solid;
    border-width: 1px;
}
.example span {
    position: absolute;
    color: #efe;
    font-family: verdana;
    font-size: 0px;
    white-space: nowrap;
    left: -999px;
    background: #333;
    /*filter: alpha(opacity=70);
    opacity: 0.7;*/
    background: rgba(0, 0, 0, 0.7);
}

Step 3. JS

Here are our main control JS file.

js/main.js

to_px = function (x) { return ''.concat(Math.round(x), 'px'); }
g_resize = function() { pgal.resize(); }

var pgal = {
    O : [], N : 0, S : 0, img : 0, span : 0, xm : 0, ym : 0, nx : 0, ny : 0, nw : 0, nh : 0,
    cx : 0, cy : 0, zoom : 1, x : 0, y : 0, z : -30000, xt : 0, yt : 0, zt : 0, 

    init : function () {
        this.cx   = this.nw / 2;
        this.cy   = this.nh / 2;
        this.img  = document.getElementById('gall').getElementsByTagName('img');
        this.span  = document.getElementById('gall').getElementsByTagName('span');
        this.N    = this.img.length;
        for (var i = 0; i < this.N; i++) this.O[i] = new this.PGObj(i);
        this.run();
        this.O[0].click();
    },
    resize : function () {
        var o   = document.getElementById('gall');
        this.nx   = o.offsetLeft;
        this.ny   = o.offsetTop;
        this.nw   = o.offsetWidth;
        this.nh   = o.offsetHeight;
        this.zoom = this.nh / 900;
    },
    run : function () {
        pgal.cx += (pgal.xm - pgal.cx) * .1;
        pgal.cy += (pgal.ym - pgal.cy) * .1;
        pgal.x  += (pgal.xt - pgal.x)  * .05;
        pgal.y  += (pgal.yt - pgal.y)  * .05;
        pgal.z  += (pgal.zt - pgal.z)  * .1;
        var i = pgal.N;
        while (i--) pgal.O[i].anim();
        setTimeout(pgal.run, 16);
    },
    PGObj : function (n) {
        this.n                = n;
        this.x                = pgal.zoom * Math.random() * pgal.nw * 3 - pgal.nw;
        this.y                = pgal.zoom * Math.random() * pgal.nh * 3 - pgal.nh;
        this.z                = Math.round(n * (10000 / pgal.N));
        this.w                = pgal.img[n].width;
        this.h                = pgal.img[n].height;
        this.oxt              = pgal.span[n];
        this.oxs              = this.oxt.style;
        this.txt              = pgal.span[n].innerHTML;
        this.oxt.innerHTML    = "";
        this.obj              = pgal.img[n];
        this.obs              = this.obj.style;
        this.obj.parent       = this;
        this.obj.onclick      = function() { this.parent.click(); }
        this.obj.ondrag       = function() { return false; }
        this.oxt.style.zIndex = this.obj.style.zIndex = Math.round(1000000 - this.z);
        this.F                = false;
        this.CF               = 100;
        this.sto              = []; 

        this.anim = function() {
            var f = 700 + this.z - pgal.z;
            if (f > 0) {
                var d               = 1000 / f;
                var X               = pgal.nw * .5 + ((this.x - pgal.x - pgal.cx) * d);
                var Y               = pgal.nh * .5 + ((this.y - pgal.y - pgal.cy) * d);
                var W               = d * this.w * pgal.zoom;
                var H               = d * this.h * pgal.zoom;
                this.obs.left       = to_px(X - W * .5);
                this.obs.top        = to_px(Y - H * .5);
                this.obs.width      = to_px(W);
                this.obs.height     = to_px(H);
                this.oxs.visibility = (this.CF-- > 0 && Math.random() > .9) ? "hidden" : "visible";
                this.oxs.left       = to_px(X - W * .5);
                this.oxs.top        = to_px(Y + H * .5);
                if ((pgal.zt - pgal.z) < 20) {
                    if (! this.F) {
                        this.F            = true;
                        this.CF           = Math.random() * 200;
                        this.oxs.fontSize = to_px(1 + d * 20 * pgal.zoom);
                        var T             = "";
                        var tn            = this.txt.length;
                        for(var i = 0; i < tn; i++) {
                            T = T.concat(this.txt.charAt(i));
                            this.sto[i] = setTimeout('pgal.O['.concat(n, '].oxt.innerHTML = "', T, '";'), Math.round(f / 4) + 10 * i);
                        }
                    }
                } else {
                    this.F = false;
                    this.oxt.innerHTML = "";
                }
            } else {
                this.x  = pgal.zoom * Math.random() * pgal.nw * 3 - pgal.nw;
                this.y  = pgal.zoom * Math.random() * pgal.nh * 3 - pgal.nh;
                this.z += 10000;
                this.oxs.zIndex = this.obs.zIndex = Math.round(1000000 - this.z);
            }
        } 

        this.cto = function() {
            var i = this.txt.length;
            while (i--) clearTimeout(this.sto[i]);
        } 

        this.click = function() {
            var i = pgal.N;
            while (i--) pgal.O[i].cto();
            if (pgal.S != this) {
                pgal.xt = this.x;
                pgal.yt = this.y;
                pgal.zt = this.z;
                pgal.S  = this;
            } else {
                pgal.S   = 0;
                pgal.zt += 1600;
            }
        }
    }
} 

// event handlers
window.onresize = g_resize;

document.onmousemove = function(e) {
    if (window.event) e=window.event;
    pgal.xm = (e.x || e.clientX) - pgal.nx - pgal.nw * .5;
    pgal.ym = (e.y || e.clientY) - pgal.ny - pgal.nh * .5;
} 

window.onload = function() {
    g_resize();
    pgal.init();
}

This is most interesting and important part of our gallery. Our photo gallery object (pgal) contain next functions: init, resize and run. During resize we re-saving all current positions of gallery object. In initialization we perform initialization of all main params and objects (images and span text objects). Each photo object (PGObj) have own set of variables, and few main functions – for animation and onclick handling.


Live Demo
download in package

Conclusion

Today we prepared interesting 3d gallery, sure that was interesting to you. If is you were wondering – do not forget to thank us. I would be grateful for your interesting comments. Good luck!

Enjoyed this Post?

    Tweet
   
   

Stay connected with us:

  • Rss Feed
  • Get Email Updates
  • Follow us on Twitter
  • Follow us on Facebook
  • Follow us on Google+
  • Follow us on LinkedIn
If you enjoyed this article, feel free to share our tutorial with your friends.
Written by: admin on April 22, 2011.
Last revised by: admin
on April 24, 2011.

Related Articles

Animated Photo Gallery (grid) with javascript

Animated Photo Gallery (grid) with javascript

Creating a Photo Scrambler grid in javascript

Creating a Photo Scrambler grid in javascript

Nice CSS3 Lightbox Gallery With jQuery

Nice CSS3 Lightbox Gallery With jQuery

TiltViewer tutorial – creating 3D flash gallery

TiltViewer tutorial – creating 3D flash gallery

12 Comments

    • Alex's Gravatar
    • AlexApril 23, 2011 4:45 am

      It’s great. Tnx

    Reply
    • Onyx's Gravatar
    • OnyxApril 23, 2011 10:09 pm

      Sweet. Interestingly the SPAN tag for image description doesn’t show in Chromium, but does in FF4 and Opera 11.10. The code is there in the Inspector, but the contents of the SPAN is removed. Huh?

    Reply
      • admin's Gravatar
      • adminApril 25, 2011 4:32 am

        2Onyx: I can`t confirm, I checked just now (again) in Chrome (recently downloaded it and install) – so, all working fine, and spans with description too.
        You also can install to Chrome plugin – firebug light, which will able to inspect most interesting places ;-)

      Reply
    • Sandeep's Gravatar
    • SandeepApril 24, 2011 10:54 am

      Truly, JavaScript can do great things. Thanks for this tutorial

    Reply
    • Marius's Gravatar
    • MariusApril 29, 2011 4:00 pm

      There is a bug, when you click on the image that is already passed but still possible to click on it. After clicking – image description remains HUGE instead of returning to normal.

    Reply
    • oskar's Gravatar
    • oskarMay 20, 2011 9:32 am

      Unbelievable that this is the whole code!
      Really impressing,

    Reply
    • Javascript Countdown Timer's Gravatar
    • Javascript Countdown TimerMay 25, 2011 3:23 am

      it’s so cool, really I’m looking for this script for my private personal blog because I have thousands of picture to share

      PS: if there’s thousands of pictures, this script will run slowly?

    Reply
    • Mark's Gravatar
    • MarkJune 30, 2011 3:32 pm

      Hi Andrey – Do you have an email address where I can write to you about this work and discuss a related project I am involved in? Thanks in advance.

    Reply
    • admin's Gravatar
    • adminJune 30, 2011 4:52 pm

      Hi Mark,
      I added Contact Us page recently – you can use this

    Reply
    • Marcos Silva's Gravatar
    • Marcos SilvaJanuary 26, 2012 2:32 am

      It´s amazing the human capacity for creating useful stuffs…great, man, great !!

      From Brazil, Paraíba state.

    Reply
    • vipin's Gravatar
    • vipinApril 15, 2012 1:52 pm

      its awsome yr really..it works

    Reply
    • peter's Gravatar
    • peterOctober 11, 2012 7:49 am

      WOW. This is awesome. Just what i’ve been looking for to use on my new project. Great piece. thumbs up!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

*

*

CAPTCHA Image
Refresh Image

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Popular Tutorials

  • Pure HTML5 file upload
  • CSS3 Modal Popups
  • How to Easily Make a PHP Chat Application
  • How to create easy pagination with jQuery
  • Form Validation with Javascript and PHP
  • CSS3 Responsive menu
  • Pure CSS3 LavaLamp Menu
  • Autocomplete with PHP, jQuery, MySQL and XML

Tags

3d AJAX animated animation app applications apps canvas captcha chat system coding creating css css3 developers dropdown effect flash forms fresh gallery game development how-to html html5 html5 games image Infographic iphone javascript jQuery layout menu navigation offer photo photo gallery PHP plugin plugins roundup tutorial tutorials website xslt

Recent Tutorials

  • Functional Programming – How to Write Functional Code in PHP
  • PHP Debugging and Profiling – Xdebug
  • Responsive Photo Gallery with LazyLoad (least.js)
  • Permalinks tutorial
  • How to create Pinterest-like script – step 6
  • WYSIWYG and WYSIWYM Editors
  • Neat and modern design – responsive
  • 15+ Fresh And Stunning CSS3 Tutorials
  • Facebook
  • Twitter
  • G+

Who is behind Script Tutorials

author

Hello there! My name is Andrew and I have been doing web development for years. Frankly, not only web-development, and system-development too. This site is the place where I get to teach and share my experience for the web.

Read more

Contact Us

Contact Us

  • Advertise
  • Contact Us
  • About
  • License

Copyright © 2009-2012 Script tutorials