Hey guys. I've got a simple javascript that rotates quotes randomly. It's very basic, so I wanted to add a fade in/ fade out transition to the text to make it more appealing. I've researched around the net and have had trouble finding sources to help me out. Here is the code.
Code:
<script language="JavaScript">
function rotateEvery(sec)
{
var Quotation=new Array()
// QUOTATIONS
Quotation[0] = '...The big brown fox jumped over the tall fence';
Quotation[1] = '...The wind is blowing cold snow across the dark black road';
Quotation[2] = '...Fall has many colors and black is not one of them';
Quotation[3] = '...the blue bird lives in the big red barn';
Quotation[4] = '....Mr. Grant really believes that the Bull’s skills, which are widespread, are utterly godlike even though he acts like he doesn’t think so';
Quotation[5] = 'Sixth quotation';
Quotation[6] = 'You can add <b>as many</b> quotations <b>as you like</b>';
var which = Math.round(Math.random()*(Quotation.length - 1));
document.getElementById('textrotator').innerHTML = Quotation[which];
setTimeout('rotateEvery('+sec+')', sec*5500);
}
</script>
quotation = new Array()
quotation[0] = "...The big brown fox jumped over the tall fence"
quotation[1] = "...The wind is blowing cold snow across the dark black road"
quotation[2] = "...Fall has many colors and black is not one of them"
quotation[3] = "...the blue bird lives in the big red barn"
quotation[4] = "...Mr. Grant really believes that the Bull’s skills, which are widespread"
quotation[5] = "...Sixth quotation"
function fadingtext(){
if(hexinput >0) {
hexinput -=11; // increase color value
document.getElementById("textrotator").style.color="rgb("+hexinput+","+hexinput+","+hexinput+")"; // Set color value.
setTimeout("fadingtext()",200); // 200ms per step
}
else {
hexinput = 255; //reset hex value
}
}
function changetext(){
if(!document.getElementById){return}
var which = Math.round(Math.random()*(quotation.length - 1)); //select random quote
document.getElementById("textrotator").innerHTML = quotation[which]; // and display it
fadingtext();
setTimeout("changetext()",5000); // five seconds
}
window.onload = changetext();
</script>
Last edited by Philip M; 12-10-2007 at 07:49 PM..
Reason: Improvement/Correct Error
function rotateQuotes() {
var q = rotateQuotes.quotes;
q = q[Math.floor(Math.random() * q.length)];
var tr = document.getElementById("textrotator");
var uf = unfade(tr, 10, 1, 0);
tr.innerHTML = q;
new TimeoutChainer({
callback: function() {
var f = fade(tr, 10, 1);
new TimeoutChainer({
callback: rotateQuotes,
runAfter: f
});
},
delay: 2000,
runAfter: uf
});
}
rotateQuotes.quotes = [
"...The big brown fox jumped over the tall fence",
"...The wind is blowing cold snow across the dark black road",
"...Fall has many colors and black is not one of them",
"...the blue bird lives in the big red barn",
"....Mr. Grant really believes that the Bull’s skills, which are widespread, are utterly godlike even though he acts like he doesn’t think so",
"sixth quotation",
"You can add <b>as many</b> quotations <b>as you like</b>"
];
rotateQuotes();
quotation = new Array();
quotation[0] = "...The big brown fox jumped over the tall fence"
quotation[1] = "...The wind is blowing cold snow across the dark black road"
quotation[2] = "...Fall has many colors and black is not one of them"
quotation[3] = "...the blue bird lives in the big red barn"
quotation[4] = "...Mr. Grant really believes that the Bull’s skills, which are widespread"
quotation[5] = "...Sixth quotation"
function fadingtext(){
if(hexinput>0) {
hexinput -=11; // increase color value
document.getElementById("textrotator").style.color="rgb("+hexinput+","+hexinput+","+hexinput+")"; // Set color value.
setTimeout("fadingtext()",200);
}
else {
hexinput=255; //reset hex value
}
}
function changetext(){
if(!document.getElementById){return}
var which = Math.round(Math.random()*(quotation.length - 1)); // randomly select a quote
document.getElementById("textrotator").innerHTML=quotation[which]; // and dispaly it
fadingtext();
setTimeout("changetext()",5000); // 5 seconds
}
window.onload=changetext();
</script>
Note that as the quote is randomly selected the same quote may appear twice in succession
Alright, I discovered the issue with the code: It needed a body tag. Btw, your id attribute should be in quotes for forward compatability.
As for my script, I had written the TimeoutChainer a week or so back, and I decided I might as well take advantage of the code I had already written
As a side note to the OP, the style="width:100%;" attribute needs to be there for an IE bug. You can change the % number though, and if you want, you can play around with the numbers in rotate.js and through trial and error find out what works best for you.
And if you really need it, I can explain my code, but it might (or might not) take a while for me to do so. Finals this week . . .
Alright, I discovered the issue with the code: It needed a body tag. Btw, your id attribute should be in quotes for forward compatability.
.... . .
Thanks guys.. sorry havent been paying attention to the thread.. i was out of town on family business.. moving the sister.. fun i know.. ill have to try these out tomorrow.. i did try yours philip but was having some trouble getting it to work.. or display at all on the page.. ill have to play with both scripts... thanks again.. means a lot that you all are helping a newb like me
Okay. I'm going to try your code again Philip, then the other so I can try and learn. I've placed the java code in its own .js file and called up the javascript in my header of my html page like the other javascript scripts i have... but i am having trouble getting the code to display in the body of the html page.. where i want the code to show its just showing nothing.. blank.. i believe i am just being stupid and cant see what i am doing wrong.. here is where i am trying to display the code... ive tried different things to call up the script.. but none seem to work.. i must be doing something obviously wrong.... i know the code i left in below is wrong.. i just wanted soemthing there so you all could see what im talking about
Glad you got it to work. Just a heads up: Java and Javascript are entirely different despite having the related names.
yea sorry about. Trinithis, I've started to look at your code as well and I've noticed that philip brings up font "style" particularly color ... with the following code:
Code:
document.getElementById("textrotator").style.color="rgb("+hexinput+","+hexinput+","+hexinput+")"; // Set color value.
Where i assume "hexinput" is where you place your hex values. Where in you code can I change color? Thinking of having a text start as the same background color, white, then go into my color of "blue steel" which is "#4682b4"
Thanks again for the reply Trinithis. I copied your code exactly, into their respective .js and .html files. Nothing happens when i load the rotate.html file into firefox.
Alright. I got the script to work. I added some minor styling edits to the code. How can I link an author to a quote. Right now they authors and quotes are appearing independently of one another. See below example.
"The big brown fox jumped over the fence"
- Little Red Riding Hood, 2007
Code:
rotateText.texts = {
quotes: [
"<strong>...The big brown fox jumped over the fence</strong>",
"<strong>...The wind is blowing cold snow across the dark black road JOHN</strong>",
"<strong>...Fall has many colors and black is not one of them POE</strong>",
"<strong>...the blue bird lives in the big red barn</strong>",
"<strong>...Mr. Grant really believes that the Bull’s skills, which are widespread</strong>",
"<strong>...Sixth quotation</strong>"
],
authors: [
"- Little Red Riding Hood, 2007",
"John",
"POE"
],
Does anyone know how to include images into this feature?
Since java script files don't allow images I was wondering if anyone knew how I could include images to go along with the quotes so that they as well fade in and out and rotate like the simple text. Thanks in advance.