Redskins Mouseover

Change the image by moving the mouse over the current image.



Washington Redskins

Richard's JavaScript Tutorials

Updated 07/18/14

Home
Animation
precachedImages
digitalClock
Object Tag (Audio)
Cookies.htm
Math_Object
Using CSS
The Style Object
Client_Info
customObject
functions
UsingArrays
comments
concatenation
documentwrite
ifelse
Loops
forms
Strings
variables

 

Using precached images in your JavaScripts

In this section I will demonstrate several different scripts. Make sure to check out the coaches slideshow below!

This script is a Redskin players script that uses precached images to display players on the team by either moving the mouse over the image or selecting a player from a drop down box.  The script also detects your browser and chooses the correct DOM event based on your browsers event model. You can find the code and explanation below the first script.

Here is the code for the above script. First the code in the head section

<script type="text/javascript"><!--
// Simple stuff declaring variables and an Array to hold the values of the players.
var cacheImages;
if (document.images)
{
var redskins = new Array("Portis", "RandleEl", "Moss", "Archuleta",
"Cooley", "Lloyd");
cacheImages = new Array(6);

// Create instances of the image object and precache the related image sources.
//** Note that the width and height of the image should be defined
for (var i = 0; i < cacheImages.length ; i++)
{
cacheImages[redskins[i]] = new Image(156,199);
cacheImages[redskins[i]].src = redskins[i] + ".jpg";
}
}

// This function sets the jpgIndex variable and calls the loadprecached() function
function rotate()
{
if (document.images)
{
var jpgIndex = document.redskinMouseover.precached.selectedIndex;
if (++jpgIndex > cacheImages.length - 1)
{
jpgIndex = 0;
}
document.redskinMouseover.precached.selectedIndex = jpgIndex;
loadprecached(document.redskinMouseover);
}
}

// This function uses the options[] array of the selection object to determine the current selection.
function loadprecached(form)
{
if (document.images)
{
var jpgIndex = form.precached.options[form.precached.selectedIndex].value;
document.images[0].src = cacheImages[jpgIndex].src;
}
}

//The crossBrowser function is where your browser detects the event model.
// Start of Mouseover Event Script
// Here the script determines your browsers event model
var MSIE = document.attachEvent ? true : false;
var DOM = document.addEventListener ? true : false;
// Now based on your browsers event model the correct statement will be used for the right event.
function crossBrowser()
{
if (MSIE)
// You will need to create an id somewhere in the body in this case the id = "players".
{
document.getElementById("players").attachEvent("onmouseover",rotate);
}
else
if (DOM)
{
document.getElementById("players").addEventListener("mouseover",rotate, false);
}
}

Now for the body of the script.

// The crossBrowser() function is called when the page is loaded so your browser type is detected immediately. The option selections for the players are listed here and note the id="players in the "a href" tag. This is done so the user can use the mouseover function.

<body onload= "crossBrowser()">
<center><h1>Redskins Mouseover</h1>
<center><form name="redskinMouseover">

<table><tr><td>
<p>Change the image by making a selection.</p>
<select name = "precached" onchange="loadprecached(this.form)">
<option value="Portis">1 - Clinton Portis</option>
<option value="RandleEl">2 - Antwaan Randle El</option>
<option value="Moss">3 - Santana Moss</option>
<option value="Archuleta">4 - Adam Archuleta</option>
<option value="Cooley">5 - Chris Cooley</option>
<option value="Lloyd">6 - Brandon Lloyd</option>
</select><br><br>

<a href="precachedImages.html" id="players">
<img alt="Washington Redskins" src="Portis.jpg"
name="Portis" height"199" width="156" border="2"/></a>
</td></tr>
</table></form></center>

That isn't too hard is it? Well now how about a slideshow using precached images?
Do you have questions about this or any other script on my site? If so then e-mail me at webmaster@cayemay.com


Redskins Coaching Staff slideshow

For this script I have used some precached Images to create a slideshow of the Washington Redskin coaches. There is a text area below the pictures used for a caption that will also rotate automatically with the pictures for each coach. The code and explanation is below the script.

Redskins Coaching Staff

Now here is the code for the above script.

// Coaches Script starts here. This is the code in the head.
//This is another example of a way to use precached images. Here an instance of the image object is being created for //each coach.

var addOne = 1;
coach1 = new Image()
coach1.src = "Gibbs.jpg"
coach2 = new Image()
coach2.src = "Saunders.jpg"
coach3 = new Image()
coach3.src = "Williams.jpg"
coach4 = new Image()
coach4.src = "Bugel.jpg"

//Now here are some variable declarations for the text that will be displayed as a caption for each coach.
textCoach1 = "Washington Redskins Head Coach / Team President, Joe Gibbs";
textCoach2 = "Washington Redskins Associate Head Coach Offense, Al Saunders";
textCoach3 = "Washington Redskins Assistant Head Coach Defense, Greg Williams";
textCoach4 = "Washington Redskins Assistant Head Coach Offense, Joe Bugel";

// The heart and soul of this script is the autoRotate() function. This function checks for the value of the addOne variable. //Then the variable is used to determine the coach and text being displayed. As the variable is incremented the next coach //and text will be displayed.

function autoRotate()
{
if (addOne<4)
{
addOne ++;
}
else
{
addOne = 1;
}
document.coaches.src=eval("coach"+addOne+".src")
document.rotateForm.coachText.value=eval("textCoach"+addOne)

// This determines the speed of the rotation for the coaches script in nano seconds. 2000 is equal to about 2 seconds, so //the coaches images and text should change every 2 seconds.
setTimeout("autoRotate()",2000);
}

function startScript()

// This simple function starts the timer and the rotation process.
{
setTimeout("autoRotate()",2000);
}

Now for the body of the script.

<h1>Redskins Coaching Staff</h1>
<form name="rotateForm"><center>

// Here the initial image and text to be displayed is determined and assigned .
<img alt="Redskins Coaching Staff" src="Gibbs.jpg" name="coaches"
height="199" width="156" border="2">
<br><INPUT TYPE="text" size=65 NAME="coachText" VALUE="Washington Redskins Head Coach / Team President, Joe Gibbs">

</form>
</center>
</body>

Now do you think you are ready to create your own precached images in your own JavaScript? Go ahead and try to modify my code to use precached images in your own JavaScript. If you have any questions about this or any other script on this site. You can e-mail me at webmaster@cayemay.com

This site was last updated 07/08/14