Fading in and out

R

Rich Sagall

I am just learning Javascript and looking for some help with having a
series of quotes that will fade in and out of the website.

Thanks,

Rich
 
E

Evertjan.

Rich Sagall wrote on 15 mei 2006 in comp.lang.javascript:
I am just learning Javascript and looking for some help with having a
series of quotes that will fade in and out of the website.

Fading on a page should be possible,
but having text fade out of a whole website,
that would be telling!

IE only:

<http://msdn.microsoft.com/library/default.asp?
url=/workshop/author/filter/reference/filters/fade.asp>

If you want more than the IE only filters, look here:

<http://www.daltonlp.com/daltonlp.cgi?item_type=1&item_id=80>

But that is not a beginners task.
 
R

Rich Sagall

Evertjan. said:
Rich Sagall wrote on 15 mei 2006 in comp.lang.javascript:


Fading on a page should be possible,
but having text fade out of a whole website,
that would be telling!

IE only:

<http://msdn.microsoft.com/library/default.asp?
url=/workshop/author/filter/reference/filters/fade.asp>

If you want more than the IE only filters, look here:

<http://www.daltonlp.com/daltonlp.cgi?item_type=1&item_id=80>

But that is not a beginners task.

I wasn't clear - not the whole page, just a small section of the website
needs to fad in and out with different quotes from users of the website.

Rich
 
E

Evertjan.

Rich Sagall wrote on 15 mei 2006 in comp.lang.javascript:
I wasn't clear - not the whole page, just a small section of the
website needs to fad in and out with different quotes from users of
the website.

It seems you have a strange definition of the word website, Rich:

A website can contain thousands of webpages.

Clientside Javascript only [mainly] acts in and on one single page.

What about these links, was that what you asked for?
 
L

Lasse Reichstein Nielsen

I am just learning Javascript and looking for some help with having a
series of quotes that will fade in and out of the website.

The easiest and most portable is to just change the color of the text
to match the background. If the background isn't a solid color, you'll
have to fade the text to transparent, which is not as widely supported
(it's not part of the current CSS standard, but will probably be in the
next one, many years from now).

To fade text from one color to the next, we'll need a function to
calculate the intermediate steps. For this I'll construct a Color-
object contructor with appropriate methods:

----

/** r,g,b must be integers in range [0,255] for toString to work */
function Color(r,g,b) {
this.r = r;
this.g = g;
this.b = b;
}
/** otherColor is a Color, degree is number in range [0..1] */
Color.prototype.blend = function blend(otherColor, degree) {
var invDegree = 1 - degree;
return new Color(Math.round(otherColor.r * degree + this.r * invDegree),
Math.round(otherColor.g * degree + this.g * invDegree),
Math.round(otherColor.b * degree + this.b * invDegree));
};
Color.prototype.toString = function toString() {
return "rgb(" + this.r + "," + this.g + "," + this.b + ")";
};

// then the animator

/**
* elem : element to apply style to
* styleProp: style property to assign color to (e.g. "color"
* or "backgroundColor")
* color1: starting color;
* color2: ending color
* steps : number of intermediate steps
* delay : number of milliseconds between steps (approx.)
* callback: optional function called when fading is complete.
* called with elem, styleProp and color2 as arguments.
*/
function fade(elem, styleProp, color1, color2, steps, delay, callback) {
var step = 0;
var id = setInterval(function() {
step++;
var newColor = color1.blend(color2, step/steps);
elem.style[styleProp] = newColor.toString();
if (step >= steps) {
clearInterval(id);
if (callback) {
callback(elem, styleProp, color2);
}
}
}, delay);
return id;
}

// then just use it: fade from black-on-white to orange-on-black in 0.5sec
fade(document.body, "color", new Color(0,0,0), new Color(0xff, 0x80, 0),
50, 10);
fade(document.body, "backgroundColor", new Color(255,255,255),
new Color(0, 0, 0), 50, 10);

---

You can the fade your quote in and out with this (assume the background is
white):
---
<blockquote id="myQuote" style="background:white;color:black;">
This is a quote
</blockquote>

<script type="text/javascript">
var quoteBlock = document.getElementById("myQuote");
var black = new Color(0,0,0);
var white = new Color(255,255,255);
var quotes = [
"This is a quote",
"This is NOT a quote",
"This is a puppy",
"Lorem ipsum etc"];

function gotoQuote(i) {
fade(quoteBlock,"color", black, white, 50, 20, function() {
quoteBlock.innerHTML = quotes; // or however it's changed;
fade(quoteBlock, "color", white, black, 50, 20, function() {
setTimeout(function() {
gotoQuote((i+1)%quotes.length);
}, 5000); // next quote after 5 sec.
})
});
}

gotoQuote(1);
</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top