Home arrow Style Sheets arrow Page 6 - Dynamic Galleries with DOM and CSS
STYLE SHEETS

Dynamic Galleries with DOM and CSS


Thumbnail galleries are one of the first things that made the Web surfing experience more interesting. These are whole pages of preview images, which, when clicked on, became the big ones allowed for fast scanning of the image material on offer and easy access to selected pictures. Chris Heilmann shows you how to create and maintain a thumbnail gallery.

Author Info:
By: Chris Heilmann
Rating: 4 stars4 stars4 stars4 stars4 stars / 22
January 31, 2005
TABLE OF CONTENTS:
  1. · Dynamic Galleries with DOM and CSS
  2. · Basic Functionality of a Thumbnail Gallery
  3. · Old School Dynamic Galleries
  4. · Sketching our Gallery
  5. · The Javascript
  6. · The Stylesheet
  7. · A Sequential Gallery
  8. · Other options

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Dynamic Galleries with DOM and CSS - The Stylesheet
(Page 6 of 8 )

There are numerous ways to style an image gallery. The technique we use here is to give the thumbnail DIVs a certain size and float them left. Then we fix the width of the “thumbs” element, which causes the thumbnails to neatly sort themselves into rows. This also means that should we resize the “thumbs” element, we won’t need to rearrange the thumbnails; they’ll do that automatically.

 

 #thumbs{

  width:700px;

 }

 #thumbs div {

  margin:5px;

  width:120px;

  height:110px;

  float:left;

 }

 #thumbs div img{

  border:none;

  display:block;

  margin:5px auto;

 }

 

For the big image we need to define the bigDynPic DIV as absolutely positioned over the others. This is the DIV that our script generates.

 

           #bigDynPic{

                      background:#369;

                      position:absolute;

                      top:1em;

                      left:80px;

                      padding:5px;

           }

 

Adding more features

So now we have it, a thumbnail gallery that displays the big picture without reloading the page or opening a pop-up, fully accessible and only available to those who really can use it. We could add a “loading” message while the image is being loaded, to avoid confusion for users on slow connections.

 

function dyngallery()

{

    var picId='bigDynPic';

    var loadingId='loadingmessage';

    var d=document.getElementById('thumbs');

    if(!d){return;}

    if(!document.getElementById(loadingId))

    {

        var lo=document.createElement('div');

        lo.appendChild(document.createTextNode('Loading image'));

        d.parentNode.insertBefore(lo,d);

        lo.id=loadingId;

        lo.style.display='none';

    }

    var piclinks=d.getElementsByTagName('a');

    for(var i=0;i<piclinks.length;i++)

    {

        piclinks[i].onclick=function()

        {

             document.getElementById(loadingId).style.display='block';

             var oldp=document.getElementById(picId);

             if(oldp)

             {

              oldp.parentNode.removeChild(oldp);

             }

             var nc=document.createElement('div');

             d.parentNode.insertBefore(nc,d);

             nc.style.display='none';

             nc.id=picId;

             var newpic=document.createElement('img');

             newpic.src=this.href;

             newpic.alt=this.getElementsByTagName('img')[0].alt;

             newpic.title='Click to return to images';

             newpic.onload=function()

             {

                 document.getElementById(loadingId).style.display='none';

             }

             newpic.onclick=function()

             {

              this.parentNode.parentNode.removeChild(this.parentNode);

             }

             nc.appendChild(newpic);

             np=document.createElement('p');

             np.appendChild(document.createTextNode(this.getElementsByTagName('img')[0].alt))

             nc.appendChild(np);

             nc.style.display='block';

             return false;

        }

    }       

}

 

Just like the big image, the loading message will need to be positioned absolutely.

 

#loadingmessage{

  position:absolute;

  top:200px;

  left:150px;

  padding:1em 5px;

  background:#ffc;

  font-family:Verdana,Sans-serif;

  font-weight:bold;

  width:20em;

  text-align:center;

  font-size:80%;

  color:#000;

}

 

This allows us to display a range of images in a slick, nice way that does not force the visitor to reload the page for every image. But what if we are dealing with a sequence of images? 


blog comments powered by Disqus
STYLE SHEETS ARTICLES

- CSS Combinators: Working with Child Combinat...
- CSS Combinators: Using General Siblings
- Intro to CSS Combinators
- CSS Semicircles and Web Page Headers
- Drawing Circular Shapes with CSS3 and Border...
- More CSS Pagination Link Templates
- CSS Pagination Links
- Animated CSS3 Image Gallery: Advanced Transi...
- CSS3 Animated Image Gallery: Transitions
- CSS3 Properties: Fixed Heights with box-sizi...
- CSS3 Properties: Altering Strokes and 3D Eff...
- CSS3 Properties: Text-Stroke
- CSS3 Transitions: Width and Height Properties
- Creating a Drop Down Menu in CSS3
- Intro to CSS Transitions

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials