Home arrow Style Sheets arrow Page 7 - 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 - A Sequential Gallery
(Page 7 of 8 )

Images that follow a sequence need another approach. For the visitor, it would be very annoying to click each of the thumbnails separately and click the real size picture to get back. A sequential viewer is needed. 

The functionality will include the following:

  • The visitor sees all images as thumbnails; clicking on one takes her to the sequential viewer.

  • The viewer displays the image in its real size and next to it the same image as a thumbnail. Below the thumbnail it displays the next one and above the previous one. 

Our script needs to do the following:

  • To avoid the need of two different style sheets, the script adds a class called “sequential” to the “thumbs” div. This means that in the style sheet we simply need to overrule the original settings with the new ones by increasing the selector’s specificity:

    thumbs div { … }

    #thumbs.sequential div {}

 

  • Loop through all the thumbnails and hide them. If the thumbnail is the one that was clicked on, it gets an extra class called “current,” and the previous and the next one do not get hidden but displayed as a block element rather than floated.

  • Add a function that reloads the entire document when the user clicks on the big image.

 

Other than that our script stays the same, we generate a DIV which will contain the big image and display when the user clicks on a thumbnail.

 

function sequentialgallery()

{

 var picId='bigDynPic';

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

 if(!d){return;}

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

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

 {

  piclinks[i].i=i;

 

  piclinks[i].onclick=function()

  {

       if(!/sequential/.test(d.className)){d.className='sequential';}

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

       {

        piclinks[j].parentNode.className=(j==this.i)?'current':'';

        piclinks[j].parentNode.style.display=(j<this.i-1 || j>this.i+1)?'none':'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.onclick=function()

       {

        window.location=window.location;

       }

       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;

  }

 }    

 

}

 

See the example with a proper style sheet in action here: http://icant.co.uk/articles/dyngallery/gallery2.html

 

The only difference in the style sheet is that we need to add the selectors for the thumbnails in the sequential view:

 

      #thumbs.sequential div{

      float:left;clear:both;margin-bottom:0;

      }

     #thumbs.sequential div.current{

          background:#036;

     }

 

The script adds the class “sequential” to the “thumbs” element when the sequential view is needed, therefore we don’t float the thumbnails in this one, but clear all floats to display them above one another. Theoretically, setting their display value to block should have done the same, but some margin-collapsing bug in Mozilla forces us to go this route. The “current” class is added to the thumbnail of the currently visible image, and all we add here is a different background color.


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 6 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials