Style Sheets
  Home arrow Style Sheets arrow Page 7 - Dynamic Galleries with DOM and CSS
IBM Rational Software Development Conference
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Download TestComplete 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
eWeek
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
STYLE SHEETS

Dynamic Galleries with DOM and CSS
By: Chris Heilmann
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 20
    2005-01-31

    Table of Contents:
  • Dynamic Galleries with DOM and CSS
  • Basic Functionality of a Thumbnail Gallery
  • Old School Dynamic Galleries
  • Sketching our Gallery
  • The Javascript
  • The Stylesheet
  • A Sequential Gallery
  • Other options

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Minimize the cost of deploying database applications. Advantage Database Server or Microsoft SQL Server – Which One is Right for You? Learn now!

    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.

    More Style Sheets Articles
    More By Chris Heilmann


       ·  Hi Chris, Nice article! I liked a lot the use of the DOM approach....
       · I liked the article a lot but a copy of the source code and style sheet would be...
       · [url]http://icant.co.uk/articles/dyngallery/dyngallery.zip[/url], however without...
       · Thanks for the link to the zip file.The source files just lets me see how it all...
       · Hi! Great article you have there! Honestly, it's my first time to see this kind of...
       · Guess I'm not the "Anonymous Loozah" :) but I have a comment on this article and the...
       · And it works perfectly here... Netscape 4.x should neither get a stylesheet nor...
       · This is a great article, but it is a pity, that the example doesnt work well in my...
       · The gallery works perfect on my firefox, on 3 different computers. Can you please...
       · It seems tha href is not removed corectly, because it starts a function but...
       · I forget to say, that the problem on my firefox could be with tab browser extension,...
       · Script works great on IE, but I can't adjust the positioning of the big pic in...
       · You got a URL where we can see the problem?
       · Here's the URL (temporarily,...
       · 1) Your pages are in quirksmode2) Your HTML is tagsoup :)...
       · Thanks for your response. The page is now fixed and validating with a HTML 4.01...
       · I love this article as it produces exactly what I am looking for. The only thing is...
       · This script was the best way to do a gallery that I could find - others seemed to...
       · Please fix the floating ads on the right side of the screen. They are floating over...
     

    STYLE SHEETS ARTICLES

    - CSS: Continuing the Clarification of CSS Cla...
    - CSS: Top Secret Classification
    - CSS: Dimensions
    - CSS: Margins and Padding
    - CSS: Crossing the Border
    - CSS: Text, Fonts, and Tables
    - CSS: Working with Text
    - CSS: Backgrounds
    - CSS for the Newbie
    - Styling Web Page Headers with Transparent Ba...
    - Creating Angled Corners with Transparent Bac...
    - Style Sheets for a Useful Links Page
    - Modifying the Look and Feel of Individual El...
    - Using Persistent Styles with Multiple Style ...
    - Working with Multiple Style Sheets






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway