Style Sheets
  Home arrow Style Sheets arrow Page 5 - 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 Javascript


    (Page 5 of 8 )

     

    Our script will follow the rules of unobtrusive Javascript. We use the Document Object Model to access our markup, create and destroy elements on the fly, and will not use any global variables. The whole script will be self-contained and should be easy to mix and match with other scripts. The only overlap is when we call it when the page has loaded. If you are not familiar with unobtrusive Javascript, you can become familiar with it by taking the self-training course to get your skills up-to-date: http://www.onlinetools.org/articles/unobtrusivejavascript/

    To apply our functionality, all we need to do is write a function that loops through all the links inside the element with the ID “thumbs,” and, to avoid the use of a pop-up window, we will generate a DIV dynamically and insert the image in that one. 

    function dyngallery()

    {

        var picId='bigDynPic';

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

        if(!d){return;}

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

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

        {

            // [..]

        }

    }

     

    Once the user clicks on any of the links inside the “thumbs” element, we want to do the following:

     

    • Check if there is already an element with the same ID as picId.

    • If that is the case, remove this element – this is necessary to make the element “shrink-wrap” around our image.

    • Create a new DIV element with the ID defined in picId.

    • Create a new image with the same source the thumbnail-link points to.

    • Set the alternative text of the image.

    • Add a title to the image, telling the visitor that clicking the image will get her back to the thumbnails.

    • Add a function to the image that removes the image when you click on it.

    • Add a paragraph below displaying the alternative text of the thumbnail.

    • Not return to the document, to avoid the real link being followed. If there is a Javascript error, the script will fail and the browser will load the big image instead.

     

    function dyngallery()

    {

        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].onclick=function()

            {

                 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()

                 {

                  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;

            }

        }       

     

    }

     

    Now all we need to do to make our gallery work is fire up our function once the page is loaded, after we checked that the browser does understand the DOM. 

    window.onload=function()

    {

        if(document.getElementById && document.createTextNode)

        {

                 dyngallery();

        }

    }

     

    Almost, that is. The functionality we developed depends on a mouse. Keyboard users can open the big image, but they cannot close it, as the newly generated DIV does not get the focus, and setting it via focus() doesn’t work in all browsers. Therefore we need to ensure that only mouse users get the functionality:

     

    window.onload=function()

    {

        if(document.getElementById && document.createTextNode)

        {

            document.body.onmouseover=function()

            {

                 dyngallery();

            }                                           

        }

    }

     

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

    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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek