Reputation: 13
I attempted to duplicate an image swap but the mouseout ends up being image 3 on each of the three swaps when I'd only like it to be on the last. Any help I can get on figuring out how I make the swaps distinct from one another so they are not calling the same image would be much appreciated, thanks!
//---imageswap1
if(document.images) {
cars1 = new Array();
cars1[1] = new Image();
cars1[1].src = "car4.png";
cars1[2] = new Image();
cars1[2].src = "car1.png";
}
function swapping_pics(picture_name, value_2) {
document.images[picture_name].src = cars1[value_2].src;
}
//---imageswap2
if(document.images) {
cars2 = new Array();
cars2[1] = new Image();
cars2[1].src = "car5.png";
cars2[2] = new Image();
cars2[2].src = "car2.png";
}
//---imageswap3
function swapping_pics(picture_name, value_2) {
document.images[picture_name].src = cars2[value_2].src;
}
if(document.images) {
cars3 = new Array();
cars3[1] = new Image();
cars3[1].src = "car6.png";
cars3[2] = new Image();
cars3[2].src = "car3.png";
}
function swapping_pics(picture_name, value_2) {
document.images[picture_name].src = cars3[value_2].src;
}
<div id="imageswap1" onMouseOver="swapping_pics('car1',1)" onMouseOut="swapping_pics('car1',2)" href="javascript:void">
<img name="car1" border=”0” src="car1.png" alt="car1">
</div>
<div id="imageswap2" onMouseOver="swapping_pics('car2',1)" onMouseOut="swapping_pics('car2',2)" href="javascript:void">
<img name="car2" border=”0” src="car2.png" alt="car2">
</div>
<div id="imageswap3" onMouseOver="swapping_pics('car3',1)" onMouseOut="swapping_pics('car3',2)" href="javascript:void">
<img name="car3" border=”0” src="car3.png" alt="car3">
</div>
Upvotes: 1
Views: 795
Reputation: 10117
You can't have multiple functions with the same name: swapping_pics
, to solve your problem, you could add an id to each function, like: swapping_pics_01
, swapping_pics_02
, swapping_pics_03
.
But this, doens't solve the mess that you have, instead of all that code, CSS can do that in a much better way ...
HTML:
<div id="imageswap1" class="swap"></div>
<div id="imageswap2" class="swap"></div>
<div id="imageswap3" class="swap"></div>
CSS:
// This class "swap" is general to all the divs
.swap {
width: 500px; // This is the image size
height: 400px;
}
#imageswap1 { background-image: url("car01.png"); }
#imageswap1:hover { background-image: url("car04.png"); } // Mouse over 1
#imageswap2 { background-image: url("car02.png"); }
#imageswap2:hover { background-image: url("car05.png"); } // Mouse over 2
#imageswap3 { background-image: url("car03.png"); }
#imageswap3:hover { background-image: url("car06.png"); } // Mouse over 3
Fiddle with the example: http://jsfiddle.net/qnw6j/
Upvotes: 1