Darkness Reigns (two-point-oh)

DHTML Effects using Javascript

Date: 2007-02-06

Switch on/off
Fade in/out
Vert. shrink/grow


This is the div being targeted with the effects

This is another div floated into the affected div to show how things change

With user-interaction becoming even more of a key focus in web development everyday (especially since the phrase Web 2.0 was coined), animating "objects" within a page has become much more common place.

Rather than rely on technologies such as Flash to animate items, DHTML can provide some impressive effects.

This is the start of a library of DHTML (Javascript based) effects that I am building - an early work in progress. Currently containing Switching on/off, fading in/out, vertically shrinking/growing.

Examples can be seen to the right - keep in mind that these are not designed to be combined so toggling a fade out followed by a grow may not function as expected.

I will be adding to this library on a regular basis and the current version (whatever that may be) can be downloaded here

The javascript functions (as at the time of writing) can be seen below:

/*
Javascript: display_functions.js
Contains functions used for display purposes (show/hide, animate, etc.)

Author: John McClumpha - http://blogs.igeek.com.au/DarkAz/ - darkaz75@gmail.com
Updated: 06/02/07

If you wish to use this code on a website, feel free 
- all I ask is for a quick email with a URL where they can be seen so I can see where/how they are being used :)

*/


// Function used to show/hide the target div
function ToggleDiv($WhichDiv) {
	if (document.getElementById($WhichDiv).style.display == "none") {
		setOpac($WhichDiv,100); // set it to opaque (just in case)
		document.getElementById($WhichDiv).style.display = "block";
	} else {
		document.getElementById($WhichDiv).style.display = "none";
	}
}

// Function used to show/hide the target div utilizing fade effects below
function FadeToggleDiv($WhichDiv) {
	if (document.getElementById($WhichDiv).style.display == "none") {
		FadeIn($WhichDiv);
	} else {
		FadeOut($WhichDiv);
	}
}

// Function used to fade "in" a div
function FadeIn($WhichDiv) {
	setOpac($WhichDiv,0); // set it to transparent
	document.getElementById($WhichDiv).style.display = "block"; // display as block just in case
	for( var i = 0 ; i <= 100 ; i++ ) {
		   setTimeout( 'setOpac(''+$WhichDiv+'',' + i + ')' , 8 * i );
	}
}

// Function used to fade "out" a div
function FadeOut($WhichDiv) {
	for( var i = 0 ; i <= 100 ; i++ ) {
		   setTimeout( 'setOpac(''+$WhichDiv+'',' + (100 - i) + ')' , 8 * i );
	}
	setTimeout('ToggleDiv(''+$WhichDiv+'')', 8 * i);
	setOpac($WhichDiv,100); // set it to opaque (just in case)
}

// Function used to set the opacity of a div (from 0 to 10)
function setOpac( $WhichDiv, value ) {
	document.getElementById($WhichDiv).style.opacity = value / 100;
	document.getElementById($WhichDiv).style.filter = 'alpha(opacity=' + value + ')';
}


// Function used to shrink/grow a div vertically
function ToggleVert($WhichDiv, $MinHeight, $MaxHeight) {
	var $CurrHeight = parseInt(document.getElementById($WhichDiv).style.height);
	if ($CurrHeight < ($MaxHeight - 10)) {
		VertScale($WhichDiv,$MaxHeight);
	} else {
		VertScale($WhichDiv,$MinHeight);
	}
}

// Function used to animate the scaling of a div vertically
function VertScale($WhichDiv, $EndHeight) {
	var $StartHeight = parseInt(document.getElementById($WhichDiv).style.height);
	document.getElementById($WhichDiv).style.display = "block"; // display as block just in case
	document.getElementById($WhichDiv).style.overflow = 'hidden'; // set the overflow to hidden
	for( var i = 0 ; i <= 50 ; i++ ) {
		   $CurrHeight = ($StartHeight - ((($StartHeight - $EndHeight) / 50) * (i-1)));
		   setTimeout( 'setHeight(''+$WhichDiv+'',' + $CurrHeight + ')' , 8 * i );
	}
	if ($EndHeight == 0) {
		setTimeout('ToggleDiv(''+$WhichDiv+'')', 8 * i);
	}
}

// Function used to set the height of a div
function setHeight( $WhichDiv, value ) {
	document.getElementById($WhichDiv).style.height = parseInt(value) + "px";
}

Darkness Reigns : personal webspace of John McClumpha - © Copyright 2007, John McClumpha
Design: Incite Graphics
Hosting: Hosted Internet Services

Total users online is: 1 of which 0 are registered users.