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


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