943,522 Members | Top Members by Rank

Ad:
Apr 6th, 2008
0

Javascript image swap on rollover.

Expand Post »
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.

javascript Syntax (Toggle Plain Text)
  1.  
  2. <script language="javascript1.5">
  3. Rollimage = new Array()
  4.  
  5. Rollimage[0] = new Image(244,244)
  6. Rollimage[0].src = "images/personal_up.png"
  7. Rollimage[1] = new Image(244,244)
  8. Rollimage[1].src = "images/personal_over.png"
  9.  
  10. Rollimage[2] = new Image(244,244)
  11. Rollimage[2].src = "images/thoughts_up.png"
  12. Rollimage[3] = new Image(244,244)
  13. Rollimage[3].src = "images/thoughts_over.png"
  14.  
  15. Rollimage[4] = new Image(244,244)
  16. Rollimage[4].src = "images/work_up.png"
  17. Rollimage[5] = new Image(244,244)
  18. Rollimage[5].src = "images/work_over.png"
  19.  
  20. function SwapBack(image, number){
  21. document.[image].src = Rollimage[number].src;
  22. return true;
  23. }
  24. function SwapOut(image, number) {
  25. document.[image].src = Rollimage[number].src;
  26. return true;
  27. }
  28.  
  29. </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.

html Syntax (Toggle Plain Text)
  1.  
  2. <div class="icons">
  3. <a href="#" onmouseover="SwapOut(personal, 0)" onmouseout="SwapBack(personal, 1)"><img name="personal" alt="logo" src="images/personal_up.png" /></a>
  4. <a href="#" onmouseover="SwapOut(thoughts, 2)" onmouseout="SwapBack(thoughts, 3)"><img name="thoughts" alt="logo" src="images/thoughts_up.png" /></a>
  5. <a href="#" onmouseover="SwapOut(work, 4)" onmouseout="SwapBack(work, 5)"><img name="work" alt="logo" src="images/work_up.png" /></a>
  6. </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.
Last edited by rickya100; Apr 6th, 2008 at 12:10 am.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
rickya100 is offline Offline
78 posts
since Mar 2008
Apr 6th, 2008
0

Re: Javascript image swap on rollover.

At a first glance, I can see that on your calling code:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div class="icons"> <a href="#" onmouseover="SwapOut(personal, 0)" onmouseout="SwapBack(personal, 1)"><img name="personal" alt="logo" src="images/personal_up.png" /></a>.... </div>

you are referencing undefined variables. Have you tried to replace the call for:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. onmouseover="SwapOut(personal, 0)"

with

onmouseover="SwapOut('personal', 0)"

as I guess you want to address the object with name 'personal' instead the object contained in personal variable.

Hope this helps.
Reputation Points: 10
Solved Threads: 4
Newbie Poster
HenryGR is offline Offline
24 posts
since Mar 2008
Apr 6th, 2008
0

Re: Javascript image swap on rollover.

Also you seem to be relying on the javascript semi-colon insertion "feature". the language does require semi-colons at the end of every statement. It's just that it will supply them for you if you omit them. This can cause bugs when it gets it wrong. they can be very hard bugs to figure out. Don't rely on it and supply all semicolons yourself. And also you might want to consider using Rollimage = []; instead of Rollimage=new Array(); . This is (i believe) faster. Because Array is a built in class, you do not need to call its constructor.
Reputation Points: 10
Solved Threads: 5
Light Poster
AaronASterling is offline Offline
31 posts
since Dec 2007
Apr 6th, 2008
0

Re: Javascript image swap on rollover.

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?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
rickya100 is offline Offline
78 posts
since Mar 2008
Apr 6th, 2008
0

Re: Javascript image swap on rollover.

Sorry mate, my earlier comments would have nothing to do with why your script fails. I was just pointing out general coding style. never rely on semi-colen insertion. as to your problem...

in your event handlers try document[image].src instead of document.[image].src
i haven't actually tried it but at least the syntax is correct
Last edited by AaronASterling; Apr 6th, 2008 at 11:37 am. Reason: fix code tags
Reputation Points: 10
Solved Threads: 5
Light Poster
AaronASterling is offline Offline
31 posts
since Dec 2007
Apr 6th, 2008
0

Re: Javascript image swap on rollover.

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
rickya100 is offline Offline
78 posts
since Mar 2008
Nov 19th, 2009
0

Image swap rollover

Hi!

Have read your thread and tried everything. But still can't make it work.

I can get the first image up but not the second or the mouseOver.

My code is:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script language="javascript1.5">
  2. Rollimage = Rollimage = [];
  3. &nbsp;
  4. Rollimage[0] = new Image(200,150)
  5. Rollimage[0].src = "automotive/images/mustangbefore.jpg"
  6. Rollimage[1] = new Image(200,150)
  7. Rollimage[1].src = "automotive/images/mustangafter.jpg"
  8. &nbsp;
  9. function SwapBack(image, number){
  10. document[image].src = Rollimage[number].src;
  11. return true;
  12. }
  13. function SwapOut(image, number) {
  14. document[image].src = Rollimage[number].src;
  15. return true;
  16. }
  17.  
  18. </script>
  19. //-->
  20. </script>
  21. </head>
  22. <body bgcolor="#FFFFFF" alink="#008000" vlink="#800080" link="#0000FF" text="#000000" onload="preloadImages()"><p>
  23.  
  24. <p>
  25.  
  26.  
  27. <a href="#" onmouseover="SwapOut('mustangafter', 0)" onmouseout="SwapBack(mustangbefore)"><img name="mustang" alt="Mustang" src="images/mustangbefore.jpg" /></a>
  28. </body>
  29. </html>
The page is at www.sodablast.net.au/automotive/test.html

The images are in the automotive sub-directory at #images.

Apologies for being a nuisance, but I can't make it work.

Looks like a good script.

Any help appreciated.

Gift in mail from Down Under (Australia) in appreciation if you can help me out.

regards

Geoff A Hinde
Last edited by nav33n; Nov 19th, 2009 at 9:26 am.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Geoff Hinde is offline Offline
6 posts
since Nov 2009
Nov 24th, 2009
0
Re: Javascript image swap on rollover.
Hi!

Have read your thread and tried everything. But still can't make it work.

I can get the first image up but not the second or the mouseOver.

My code is:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script language="javascript1.5">
  2. Rollimage = Rollimage = [];
  3. &nbsp;
  4. Rollimage[0] = new Image(200,150)
  5. Rollimage[0].src = "automotive/images/mustangbefore.jpg"
  6. Rollimage[1] = new Image(200,150)
  7. Rollimage[1].src = "automotive/images/mustangafter.jpg"
  8. &nbsp;
  9. function SwapBack(image, number){
  10. document[image].src = Rollimage[number].src;
  11. return true;
  12. }
  13. function SwapOut(image, number) {
  14. document[image].src = Rollimage[number].src;
  15. return true;
  16. }
  17.  
  18. </script>
  19. //-->
  20. </script>
  21. </head>
  22. <body bgcolor="#FFFFFF" alink="#008000" vlink="#800080" link="#0000FF" text="#000000" onload="preloadImages()"><p>
  23.  
  24. <p>
  25.  
  26.  
  27. <a href="#" onmouseover="SwapOut('mustangafter', 0)" onmouseout="SwapBack(mustangbefore)"><img name="mustang" alt="Mustang" src="images/mustangbefore.jpg" /></a>
  28. </body>
  29. </html>
The page is at www.sodablast.net.au/automotive/test.html

The images are in the automotive sub-directory at #images.

Apologies for being a nuisance, but I can't make it work.

Looks like a good script.

Any help appreciated.

Gift in mail from Down Under (Australia) in appreciation if you can help me out.

regards

Geoff A Hinde
Some lines within your code call my attention:
Rollimage = Rollimage = [];
should be:
Rollimage = new Array();
then the two lines with "&nbsp;" are meaningless, please remove them.
The two functions (SwapOut and SwapBack) are exactly he same code... Why to have them dplicated instead of use just one of them?

Also, you are using only one image in your document, with the name attribute of 'mustang', but you are trying to find images with the names of "mustangbefore" and "mustangafter".

The call to any of the functions that you have, should be something like:
onmouseover="SwapOut('mustang', 0)"
where 'mustang' is the name of the image (within the HTML document body) that you want to alter, and 0 is the number of the matrix index that you want to place in.

Within the functions that you have, you attempt to address the image using:
document[image].src = Rollimage[number].src;
and, in order to properly address the image that you want to change, the line should be:
document.getElementById(image).src = Rollimage[number].src;
Reputation Points: 10
Solved Threads: 4
Newbie Poster
HenryGR is offline Offline
24 posts
since Mar 2008
Nov 25th, 2009
0
Re: Javascript image swap on rollover.
Henry - thanks.

Have followed your advice so far but still does not seem to work.

Code now is:

<script language="javascript1.5">
Rollimage = new Array();
Rollimage[0] = new Image(200,150)
Rollimage[0].src = "automotive/images/mustangbefore.jpg"
Rollimage[1] = new Image(200,150)
Rollimage[1].src = "automotive/images/mustangafter.jpg"
function SwapOut(image, number) {
document.getElementById(image).src = Rollimage[number].src;
return true;
}
</script>

And call is

<a href="#" onmouseover="SwapOut('mustangafter.jpg', 1)" onmouseout="SwapOut('mustangbefore.jpg', 0)"><img name="mustang" src="images/mustangbefore.jpg" /></a>

Test page updated.

Would greatly appreciate it if you could tweak this for me.

Much thanks

Geoff
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Geoff Hinde is offline Offline
6 posts
since Nov 2009
Dec 4th, 2009
0
Re: Javascript image swap on rollover.
Henry - thanks.

Have followed your advice so far but still does not seem to work.

Code now is:

<script language="javascript1.5">
Rollimage = new Array();
Rollimage[0] = new Image(200,150)
Rollimage[0].src = "automotive/images/mustangbefore.jpg"
Rollimage[1] = new Image(200,150)
Rollimage[1].src = "automotive/images/mustangafter.jpg"
function SwapOut(image, number) {
document.getElementById(image).src = Rollimage[number].src;
return true;
}
</script>

And call is

<a href="#" onmouseover="SwapOut('mustangafter.jpg', 1)" onmouseout="SwapOut('mustangbefore.jpg', 0)"><img name="mustang" src="images/mustangbefore.jpg" /></a>

Test page updated.

Would greatly appreciate it if you could tweak this for me.

Much thanks

Geoff
Geoff,
please alter your call to:

<a href="#" onmouseover="SwapOut('mustang', 1)" onmouseout="SwapOut('mustang', 0)"><img name="mustang" src="images/mustangbefore.jpg" /></a>

Rgds.
Reputation Points: 10
Solved Threads: 4
Newbie Poster
HenryGR is offline Offline
24 posts
since Mar 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Changing alert's title
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: onchange retain POST value when submitted





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC