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