Originally published February 2007 - http://blogs.igeek.com.au/DarkAz/
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.
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
// display as block just in case
document.getElementById($WhichDiv).style.display = "block";
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);
// display as block just in case
document.getElementById($WhichDiv).style.display = "block";
// set the overflow to hidden
document.getElementById($WhichDiv).style.overflow = '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";
}