Home arrow JavaScript arrow Page 3 - Building Dynamic Shadows for an Image Gallery with JavaScript and CSS
JAVASCRIPT

Building Dynamic Shadows for an Image Gallery with JavaScript and CSS


If you're looking for a way to get realistic shading effects on your web site with some JavaScript and CSS, you've come to the right place. This third part of a three-part series applies what you learned in the first two parts to adding dynamic shadows to a simple image gallery.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
July 13, 2009
TABLE OF CONTENTS:
  1. · Building Dynamic Shadows for an Image Gallery with JavaScript and CSS
  2. · Review: key concepts for creating dynamic shadows
  3. · Working with an image gallery
  4. · Source code of the image shading application

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Building Dynamic Shadows for an Image Gallery with JavaScript and CSS - Working with an image gallery
(Page 3 of 4 )

To implement this DOM-based approach for building dynamic shadows in the context of a real-word example, I'm going to define a sample (X)HTML file, which will contain a few example images.

Them, with the help of JavaScript and CSS, I'm going to incorporate a dynamic shadow into each of those images, so you can see how this technique can be applied in a truly useful fashion.

But, for the moment, pay attention to the signature of the (X)HTML file below, which is responsible for displaying the images on screen:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Adding dynamic shadows to images with JavaScript</title>

<style type="text/css">

body{

padding: 0;

margin: 0;

background: #fff;

}

h1{

font: bold 14pt Arial, Helvetica, sans-serif;

color: #000;

text-align: center;

}

.container{

position: relative;

width: 263px;

height: 150px;

margin-bottom: 20px;

margin-left: auto;

margin-right: auto;

z-index: 2;

}

.absimage{

position: absolute;

top: 0px;

left: 0px;

border: 1px solid #999;

z-index: 1;

}

</head>

<body>

<h1>Adding dynamic shadows to images with JavaScript</h1>

<div class="container"><img src="img1.jpg" width="263" height="150" class="absimage" /></div>

<div class="container"><img src="img2.jpg" width="263" height="150" class="absimage" /></div>

<div class="container"><img src="img3.jpg" width="263" height="150" class="absimage" /></div>

</body>

</html>

 

As shown previously, the above (X)HTML file includes three sample images, which also are wrapped by some additional DIVs, identified generically as "container." Also, you should notice that each of these images is absolutely positioned with reference to the wrapping DIVs, which will facilitate the inclusion of the pertinent dynamic shadows at a later time.

In addition, to complement the previous explanation, below I included all the sample images coded before. Here they are:

 

 

 

 

So far, so good. At this stage I've built a primitive image gallery to test out the JavaScript shading application developed earlier. Next, I'm going to slightly modify the signature of the "addShadow()" JavaScript function, to provide it with the ability to add a dynamic shadow to each picture of the gallery.

The brand new definition of this function is as follows:

 

function addImageShadows(){

var images=document.getElementsByTagName('img');

if(!images){return};

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

if(images[i].parentNode.className=='container'){

//images[i].parentNode.style.zIndex=10-i;

// create shadow

var shadow=document.createElement('div');

shadow.className='shadow';

// append shadow to image

images[i].parentNode.appendChild(shadow);

}

}

}

 

As I explained earlier, now I've defined a new JavaScript function, called "addImageShadow()" that has the capacity to add a dynamic shadow to each image of the gallery. In this case, the function iterates over each image, and appends to it the pertinent shadow, via the DOM. That was really simple to understand, wasn't it?

Of course, the previous function should be called after the web page has been loaded, as shown below:

 

// add dynamic shadows to images after web page has been loaded

window.onload=function(){

if(document.createElement&&document.getElementById&&document. getElementsByTagName){

addImageShadows();

}

}

 

Now that you've grasped the logic that drives the prior "addImageShadow()" JavaScript function, you might want to see how these sample images look after incorporating into them the respective shadows. Therefore, below I included the "shadowed" version of them. Here they are:

 

 

 

 

See how easy it is to build lightweight shadows with JavaScript? I told you it was a simple process! In this case, I used this approach with some sample images, but the same concept can be extended to other web page elements as well.

Having demonstrated how the previous "addImageShadow()" JavaScript function does its thing, it's time to pick up all the pieces that compose this shading application and merge them into one single file.

Naturally, this will be done in the last section of this tutorial, so click on the link shown below and keep reading.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

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