Animate dynamic element h1 tag : Animation « GUI Components « JavaScript DHTML

Animate dynamic element h1 tag


Examples From
JavaScript: The Definitive Guide, Fourth Edition

Legal matters: these files were created by David Flanagan, and are
Copyright (c) 2001 by David Flanagan.  You may use, study, modify, and
distribute them for any purpose.  Please note that these examples are
provided "as-is" and come with no warranty of any kind.

David Flanagan
<!-- This is the dynamic element we will animate.  We wrap the h1 tag in a -->
<!-- div because IE 4 won't move the h1 without a div or a span container. -->
<div id="title" style="position:absolute"><h1>Hello</h1></div>

<!-- This is the JavaScript code that performs the animation -->
// These variables set the parameters for our animation
var id = "title";                  // Name of the element to animate
var numFrames = 30;                // How many frames to display
var interval = 100;                // How long to display each frame
var x0 = 100, y0 = 100;            // The element's starting position
var x1 = 500, y1 = 500;            // The element's ending position
var dx = (x1 - x0)/(numFrames-1);  // Distance to move horizontally each frame
var dy = (y1 - y0)/(numFrames-1);  // Distance to move vertically each frame
var frameNum = 0;                  // Frame we are at now
var element = null;                // The element to be animated

// First, we find the element to be animated.  Use a DOM-compliant technique
// if the browser supports it, otherwise fall back on browser-specific code.
if (document.getElementById) {     // If this is a DOM-compliant browser
    element = document.getElementById(id);  // Use the DOM method
else if (document.all) {           // Otherwise, if the IE API is supported
    element = document.all[id];    // Use the all[] array to find the element
else if (document.layers) {        // Else if the Netscape API is supported
    element = document.layers[id]; // Use the layers[] array to get the element

// If we found the element to animate using one of the techniques above,
// start animating it by calling nextFrame() every interval milliseconds.
if (element) {
    var intervalId = setInterval("nextFrame()", interval);

// This function is called repeatedly to display each frame of the animation.
// It moves the element using either the DOM API for setting CSS style
// properties, or, if the browser does not support that API, it uses
// the Netscape Layer API.
function nextFrame() {
    if ( {
        // If the browser supports it, move the element by setting CSS
        // style properties.  Note the inclusion of the units string. = x0 + dx*frameNum + "px"; = y0 + dy*frameNum + "px";
    else {
        // Otherwise, assume that element is a Layer, and move it by
        // setting its properties.  We could also use element.moveTo().
        element.left = x0 + dx*frameNum; = y0 + dy*frameNum;

    // Increment the frame number, and stop if we've reached the end.
    if (++frameNum >= numFrames) clearInterval(intervalId);

Related examples in the same category

1.Attack animation
2.Circle Animation
3.Right to left animation
4.Flash animation in Javascript
5.Flash animation in JavaScript: Changing style Properties
6.Animation along Straight Line
7.Animation along a Circle
8.Dancing Text (IE)
9.Type Writer effect (IE)
10.Type Writer Effect 1.1 (IE)
11.JavaScript Ticker 1.2 (IE)
12.Animation: Lottery Number Picker and Statistics
13.Animation: wriggly
14.Animation: welcome message
15.Animation: trio
16.Auto lotto dip
17.Animation: snow
18.Animation: star
19.Animation: mouse doodle
20.Animation: fireworks
21.Animation: pretty
22.Animation: Random Movement
23.Lotto number draw
24.Following eyes
25.Animation: three eyes
26.Animation: eyes
27.Spot light
28.Big static eyes
29.Animation based on DIV with color flash
30.Framework for creating CSS-based animations
31.Popup window animation (fly across screen)
32.Animation on several images
33.Using the onFilterChange Event Handler
34.JavaScript Animation
35.Periodically Updating the Text Displayed by an HTML Element
36.Moving an Airplane Across a Web Page
37.Link Hint Scroller 2.0 (IE)
38.Snow animation
39.Animation with JSTween