这是一个教科书里的例子,自己体会吧
<html>
<head>
<title>Listing 32.3. Fading Text</title>
</head><body bgcolor="white" onLoad="initialize()"><div id="faderelement"
style="position:absolute;
width: 300px;
height: 60px;
left: 10px;
top: 50px;
visibility:hidden">
</div><script language="JavaScript" type="text/javascript">
<!--// Use these variables to configure the scroller
var fade_delay = 50
var fadein_speed = 5
var fadeout_speed = 10
var pause_delay = 2000
var maximum_loops = 0// Add your messages here
var messages = new Array()
messages[0] = "Be sure to visit the <a href='http://www.wordspy.com/'>" +
"Word Spy Web site</a> for the latest word on the newest words."
messages[1] = "Check out <a href='http://www.logophilia.com/WordPlay/" +
"WordProspector.html'>Word Prospector</a>, the weekly word "+
"tournament where you match wits with fellow logophiles."
messages[2] = "Word and language fans will love <a href='http://www." +
"logophilia.com/waw/'>Words About Words</a>, a treasure trove " +
"of word- and language-related quotations."// Don't edit anything below here
var timeout_id
var current_color = 255
var fading_in = true
var fading_out = false
var loop_counter = 1
var message_counter = 0
var faderfunction initialize() { // If this is a non-DOM browser, bail out
if (!document.getElementById) { return }
// Get the <div> element
fader = document.getElementById("faderelement")
// Initialize the fader
initialize_fader()}// This function sets the initial scroller position and clip region
function initialize_fader() { // Hide the fader
fader.style.visibility = "hidden" // Set the message
fader.innerHTML = messages[message_counter]
// Start fading
fade_it()
}// This function fade the text in and out
function fade_it() {
if (fading_in || fading_out) {
// Convert the red, green, and blue values to hexadecimal
var red_hex = decimal_to_hex(current_color)
var green_hex = red_hex
var blue_hex = red_hex
// Set the color style
fader.style.color = "#" + red_hex + green_hex + blue_hex
// Does the fader have children?
if (fader.hasChildNodes()) {
// If so, loop through them change the color style for each
for (var counter = 0; counter < fader.childNodes.length; counter++) {
// We only need to work with elements
if (fader.childNodes[counter].nodeType == 1) {
fader.childNodes[counter].style.color = "#" + red_hex + green_hex + blue_hex
}
}
}
// Show the fader
fader.style.visibility = "visible"
// Adjust the color value for the fade-in portion
if (fading_in) {
current_color -= fadein_speed
// Has the text faded in completely?
if (current_color < 0) {
// If so, reset the color
current_color = 0
// Set up the flags for the fade out
fading_in = false
fading_out = true
// Leave the text onscreeen for pause_delay ms
timeout_id = setTimeout("fade_it()", pause_delay)
return
}
} // Adjust the color value for the fade-out portion
if (fading_out) {
current_color += fadeout_speed
// Has the text faded out completely?
if (current_color > 255) {
// If so, set up the flags for the fade in
fading_in = false
fading_out = false
}
} // Do it again
timeout_id = setTimeout("fade_it()", fade_delay)
}
else {
// Otherwise, the text has faded in and faded out,
// so reset the color and the flag
current_color = 255
fading_in = true // Increment and check message_counter
if (++message_counter == messages.length) {
// If we've gone past the last message, start over
message_counter = 0
loop_counter++
} // Check the loop count
if (loop_counter <= maximum_loops || maximum_loops == 0) {
// If it's okay, start the fader back at the beginning
initialize_fader()
}
}
}function decimal_to_hex(decimal_value) {
var hex_array = new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F")
var hex_index1 = Math.floor(decimal_value / 16)
var hex_index2 = decimal_value % 16
var hex1 = hex_array[hex_index1]
var hex2 = hex_array[hex_index2]
return hex1 + hex2
}
//-->
</script></body>
</html>
<html>
<head>
<title>Listing 32.3. Fading Text</title>
</head><body bgcolor="white" onLoad="initialize()"><div id="faderelement"
style="position:absolute;
width: 300px;
height: 60px;
left: 10px;
top: 50px;
visibility:hidden">
</div><script language="JavaScript" type="text/javascript">
<!--// Use these variables to configure the scroller
var fade_delay = 50
var fadein_speed = 5
var fadeout_speed = 10
var pause_delay = 2000
var maximum_loops = 0// Add your messages here
var messages = new Array()
messages[0] = "Be sure to visit the <a href='http://www.wordspy.com/'>" +
"Word Spy Web site</a> for the latest word on the newest words."
messages[1] = "Check out <a href='http://www.logophilia.com/WordPlay/" +
"WordProspector.html'>Word Prospector</a>, the weekly word "+
"tournament where you match wits with fellow logophiles."
messages[2] = "Word and language fans will love <a href='http://www." +
"logophilia.com/waw/'>Words About Words</a>, a treasure trove " +
"of word- and language-related quotations."// Don't edit anything below here
var timeout_id
var current_color = 255
var fading_in = true
var fading_out = false
var loop_counter = 1
var message_counter = 0
var faderfunction initialize() { // If this is a non-DOM browser, bail out
if (!document.getElementById) { return }
// Get the <div> element
fader = document.getElementById("faderelement")
// Initialize the fader
initialize_fader()}// This function sets the initial scroller position and clip region
function initialize_fader() { // Hide the fader
fader.style.visibility = "hidden" // Set the message
fader.innerHTML = messages[message_counter]
// Start fading
fade_it()
}// This function fade the text in and out
function fade_it() {
if (fading_in || fading_out) {
// Convert the red, green, and blue values to hexadecimal
var red_hex = decimal_to_hex(current_color)
var green_hex = red_hex
var blue_hex = red_hex
// Set the color style
fader.style.color = "#" + red_hex + green_hex + blue_hex
// Does the fader have children?
if (fader.hasChildNodes()) {
// If so, loop through them change the color style for each
for (var counter = 0; counter < fader.childNodes.length; counter++) {
// We only need to work with elements
if (fader.childNodes[counter].nodeType == 1) {
fader.childNodes[counter].style.color = "#" + red_hex + green_hex + blue_hex
}
}
}
// Show the fader
fader.style.visibility = "visible"
// Adjust the color value for the fade-in portion
if (fading_in) {
current_color -= fadein_speed
// Has the text faded in completely?
if (current_color < 0) {
// If so, reset the color
current_color = 0
// Set up the flags for the fade out
fading_in = false
fading_out = true
// Leave the text onscreeen for pause_delay ms
timeout_id = setTimeout("fade_it()", pause_delay)
return
}
} // Adjust the color value for the fade-out portion
if (fading_out) {
current_color += fadeout_speed
// Has the text faded out completely?
if (current_color > 255) {
// If so, set up the flags for the fade in
fading_in = false
fading_out = false
}
} // Do it again
timeout_id = setTimeout("fade_it()", fade_delay)
}
else {
// Otherwise, the text has faded in and faded out,
// so reset the color and the flag
current_color = 255
fading_in = true // Increment and check message_counter
if (++message_counter == messages.length) {
// If we've gone past the last message, start over
message_counter = 0
loop_counter++
} // Check the loop count
if (loop_counter <= maximum_loops || maximum_loops == 0) {
// If it's okay, start the fader back at the beginning
initialize_fader()
}
}
}function decimal_to_hex(decimal_value) {
var hex_array = new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F")
var hex_index1 = Math.floor(decimal_value / 16)
var hex_index2 = decimal_value % 16
var hex1 = hex_array[hex_index1]
var hex2 = hex_array[hex_index2]
return hex1 + hex2
}
//-->
</script></body>
</html>
注释有的,根据需求改几个变量的值就可以了