Home arrow JavaScript arrow Page 4 - Using the DOM to Build Dynamic Shadows with JavaScript and CSS
JAVASCRIPT

Using the DOM to Build Dynamic Shadows with JavaScript and CSS


A proper combination of JavaScript code and CSS styles can be the perfect recipe for incorporating eye-catching effects into a certain number of elements included in a given web document. This three-part series of articles walks you through creating dynamic shadows using unobtrusive client-side scripting, so if you’re interested in learning how to implement this visual effect within your own web pages, then you’ve come to the right place.

Author Info:
By: Alejandro Gervasio
Rating:  stars stars stars stars stars / 0
July 06, 2009
TABLE OF CONTENTS:
  1. · Using the DOM to Build Dynamic Shadows with JavaScript and CSS
  2. · Reviewing some previous shadowing techniques
  3. · Creating dynamic shadows with the DOM
  4. · The shading application’s full source code

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Using the DOM to Build Dynamic Shadows with JavaScript and CSS - The shading application’s full source code
(Page 4 of 4 )

As I said in the section that you just read, here’s the complete source code corresponding to this simple JavaScript application, which uses some basic CSS styles along with the DOM to incorporate a dynamic shadow into a given web page element:

 

<!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>

That’s all for the moment concerning the creation of dynamic shadows with the DOM. Of course, feel free to introduce your own modifications to all of the code samples developed in this tutorial, to improve your existing skills in incorporating this visual effect into your web sites.

Final thoughts

In this second tutorial of the series, you hopefully learned the basics of using the DOM, along with some basic CSS styles, to build dynamic shadows, which can be easily attached to any element of a web document.

Nonetheless, it’s necessary to implement this approach in the context of a real-world situation. In the last part of this series, then, I’m going to teach you how to incorporate this simple shading effect into an existing image gallery.

Now that you know what the upcoming tutorial will be about, you won’t want to miss it!


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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