Style Sheets
  Home arrow Style Sheets arrow Page 6 - Dynamic Galleries with DOM and CSS
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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 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 / 22
    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
     
     
    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? 

    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...
       · hiya,great script, looks really good - only problem is that when I run in IE7...
     

    STYLE SHEETS ARTICLES

    - Image Replacement CSS Techniques
    - Using BlueTrip`s Success, Notice and Error C...
    - More Uses for the Thin and Caps CSS Classes ...
    - Styling Definition Lists with the BlueTrip C...
    - Styling Unordered and Ordered HTML Lists wit...
    - Using the BlueTrip CSS Framework`s Thin and ...
    - Adding Borders to Web Page Columns with Blue...
    - Introducing the BlueTrip CSS Framework
    - Using a Background Grid to Assist Web Page L...
    - Extending the Rule Of Thirds for Web Page La...
    - A Two-Column Web Page Layout Based on the Ru...
    - Using the Rule Of Thirds for Web Page Layout
    - Swapping Columns Using the Divine Ratio for ...
    - Using the Golden Ratio in Liquid Web Page De...
    - Fundamental Design Principles for Web Page L...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek