Home arrow JavaScript arrow Page 2 - 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 - Review: key concepts for creating dynamic shadows
(Page 2 of 4 )

In this case, before I start showing you how to incorporate some dynamic shadows into a group of existing images, I'm going to reintroduce the approach that I developed in the preceding article of the series. This will help you remember how a basic shading effect was attached to a single DIV.

Please take a look at the following sample (X)HTML file. It uses a combination of the DOM and some CSS styles to incorporate a neat shadow into a selected DIV. The corresponding code sample is as follows:

<!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>Example of shadowed DIV using JavaScript (fixed size)</title>

<script language="javascript">

function addShadow(){

var mainCont=document.getElementById('maincontainer');

if(!mainCont){return};

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

mainCont.appendChild(shadow);

shadow.id='shadow';

}

window.onload=function(){

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

addShadow();

}

}

</script>

<style type="text/css">

body{

padding: 0;

margin: 0;

background: #fff;

}

h1{

text-align: center;

margin: 0;

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

color: #000;

}

p{

font: normal 10pt Arial, Helvetica, sans-serif;

color: #000;

margin: 0;

}

#maincontainer{

position: relative;

width: 350px;

margin-left: auto;

margin-right: auto;

z-index: 1;

}

#textcontainer{

position: absolute;

padding: 10px;

background: #ccc;

z-index: 2;

}

#shadow{

position: absolute;

left: 4px;

top: 4px;

width: 340px;

height: 310px;

background: #999;

z-index: 0;

}

</style>

</head>

<body>

<h1>Example of shadowed DIV using JavaScript (fixed size)</h1>

<div id="maincontainer">

<div id="textcontainer">

<p>Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here. Content for DIV element goes here.</p>

</div>

</div>

</body>

</html>

 

As you can see, the above example illustrates how to incorporate a simple -- yet attractive -- shading effect into a given DIV. In this case, I defined a simple JavaScript function called "addShadow()," which is responsible for adding the shadow in question to a DIV, identified as "textcontainer."

This task is performed via the DOM, since the function absolutely positions the "shadow" DIV behind the selected element. In this way it achieves the visual effect without using an additional background image.

At this point, everything looks good; you've learned how to build lightweight shadows by using the DOM along with some basic CSS styles. Nevertheless, this educational journey hasn't finished yet. Now I'd like to show you how to use this technique in the context of a real-word situation. Therefore, in the following section I'll show you how to incorporate these dynamic shadows into a simple image gallery.

To learn how this will be done, please jump ahead and read the following lines.


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