Javascript image swap on rollover.
Hi and thanks for looking.
The problem i'm having is simply getting an image to swap on a mouseover. Well I sort of had it working but then I cahnged the function to try and accomodate for more images and it just fell over dead.
Let me show you the function I have for it. Bear in mind I'm used to coding but more PHP and VB rather than JavaScript so the code ain't going to be pretty, sorry.
<script language="javascript1.5">
Rollimage = new Array()
Rollimage[0] = new Image(244,244)
Rollimage[0].src = "images/personal_up.png"
Rollimage[1] = new Image(244,244)
Rollimage[1].src = "images/personal_over.png"
Rollimage[2] = new Image(244,244)
Rollimage[2].src = "images/thoughts_up.png"
Rollimage[3] = new Image(244,244)
Rollimage[3].src = "images/thoughts_over.png"
Rollimage[4] = new Image(244,244)
Rollimage[4].src = "images/work_up.png"
Rollimage[5] = new Image(244,244)
Rollimage[5].src = "images/work_over.png"
function SwapBack(image, number){
document.[image].src = Rollimage[number].src;
return true;
}
function SwapOut(image, number) {
document.[image].src = Rollimage[number].src;
return true;
}
</script>
Thats my function which i have placed in a PHP file which I then include on everypage.
Now the 3 image links that I am calling the function from.
<div class="icons">
<a href="#" onmouseover="SwapOut(personal, 0)" onmouseout="SwapBack(personal, 1)"><img name="personal" alt="logo" src="images/personal_up.png" /></a>
<a href="#" onmouseover="SwapOut(thoughts, 2)" onmouseout="SwapBack(thoughts, 3)"><img name="thoughts" alt="logo" src="images/thoughts_up.png" /></a>
<a href="#" onmouseover="SwapOut(work, 4)" onmouseout="SwapBack(work, 5)"><img name="work" alt="logo" src="images/work_up.png" /></a>
</div>
So that's it. As i say I had it working with only one image but trying to expand upon the example i found hasn't worked.
Any tips or help that you could post would be great.
Thanks for looking.
BTW I have searched google and read many articles on this but still can't get it to work. Also I have searched previous articles on DaniWeb and again none solve the problem.
The address of the page is www.richardjohnashe.com/portfolio .
rickya100
Junior Poster in Training
78 posts since Mar 2008
Reputation Points: 13
Solved Threads: 1
HenryGR and AaronASterling, thanks both for the advice, unfortunately it still doesn't work when I enclose the variables in quotation marks, when I add a semi colon to the end of lines or when I replace the 'new array' text with just the square brackets [].
If there is anything else you think is posibly wrong, however small it would be great if you could post it.
And i assume that in the code I don't have to actually explicitly create the variables 'image' and 'number'. Or do I?
rickya100
Junior Poster in Training
78 posts since Mar 2008
Reputation Points: 13
Solved Threads: 1
AaronASterling u got it!
GREAT!!!
Removing the .from between the document and square brackets got it. I also had to swap my numbers that I'm passing around bit to get the right order of swaps but you got it.
Man thanks very much for the time you spent. Fantastic. O and also thanks to henryGR as well, thanks man.
rickya100
Junior Poster in Training
78 posts since Mar 2008
Reputation Points: 13
Solved Threads: 1