/    Sign up×
Community /Pin to ProfileBookmark

Multiple Image Swap

HI,

Here is the site page I am working on so you can see the issue. [URL=http://www.thescrapbooksite.com/default2.asp]www.thescrapbooksite.com/default2.asp[/URL]

If you view this my explanation of my issue will make more sense. You could also view source to help….I’ll also include code below.

WHAT I AM TRYING TO DO:
I have an image that I want to roll over (February Kit 1 Resolutions – image name “kitone_off.gif”) and I need that image to change to image name “kitone_on.gif”. It also need to add a hyperlink to it if clicked(kit.asp). At the same time this happens I need the large image of the boy(owen.gif)to also change to a new image (kit2rollover.jpg).

ISSUE/ERROR:
I can always get one to work but not both. The original images show fine when you are first at the site, on the rollover it displays as if the images are not there and does not return to original image. I get no javascrip error. Yes, the image are placed in the correct images folder. (maybe I need to direct the javascript to the images folder…but not sure)

Here is the javascript at the top of the html page.

[code=html]
<script language=”JavaScript” type=”text/JavaScript”>
<!– Begin

if (document.images)
{
pic1on= new Image(100,25);
pic1on.src=”kitone_on.gif”;
pic2on= new Image(100,25);
pic2on.src=”owen.gif”;
pic3on= new Image(100,25);
pic3on.src=”kitone_one.gif”;

pic1off= new Image(100,25);
pic1off.src=”kitone_off.gif”;
pic2off= new Image(100,25);
pic2off.src=”kit2rollover.jpg”;
pic3off= new Image(100,25);
pic3off.src=”kitone_off.gif”;
}

function lightup(imgName,imgName2)
{
if (document.images)
{
imgOn=eval(imgName + “on.src”);
document[imgName].src= imgOn;
imgOn2=eval(imgName2 + “on.src”);
document[imgName2].src= imgOn2;
}
}

function turnoff(imgName,imgName2)
{
if (document.images)
{
imgOff=eval(imgName + “off.src”);
document[imgName].src= imgOff;
imgOff2=eval(imgName2 + “off.src”);
document[imgName2].src= imgOff2;
}
}

function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf(“#”)!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//–>
</script>
[/code]

And here is the html code down in the page:

[code=html]<td><a href = “kit.asp” onMouseover=”lightup(‘pic1′,’pic2’)” onMouseout=”turnoff(‘pic1′,’pic2’)”><img src=”images/kitone_off.gif” alt=”February Kit 1 Resolutions” name=”pic1″ width=”140″ height=”140″ id=”pic1″></a></td>[/code]

You would be my hero if you help me out….I offered to help a friend out on this site (for free) of course and I can’t tell you how long I have been working on just this little bugger. Not to mention I am a newbie. :rolleyes:

thanks!

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@KorJan 21.2006 — Try this:

the javascript code:
[code=php]
<script type="text/javascript">
var rel ='images/';//relative path of the pictures
var proll=['owen.gif','kit2rollover.jpg']//off/on
function roll(obj,id,ind){
var url=obj.getAttribute('src').split('/')[obj.getAttribute('src').split('/').length-1];
url=(url.indexOf('_off')>-1)?url.replace('_off','_on'):url.replace('_on','_off');
obj.setAttribute('src',rel+url);
document.getElementById(id).setAttribute('src',rel+ind);
}
</script>
[/code]

The HTML modifies:
[code=php]
...
<a href="kit.asp" onmouseover="roll(this.firstChild,'pic2',proll[1])" onmouseout="roll(this.firstChild,'pic2',proll[0])"><img src="images/kitone_off.gif" alt="February Kit 1 Resolutions" width="140" height="140"></a>
......
<img src="images/owen.gif" id="pic2" width="296" height="296">
...
[/code]
Copy linkTweet thisAlerts:
@artgrrlauthorJan 21.2006 — HI! Thanks so much that worked absolutely perfect!
Copy linkTweet thisAlerts:
@artgrrlauthorJan 21.2006 — Now that this is working perfect I want to add a couple more rollovers that do the same thing on another area of the same page and with different images.

To do this do I need to replicate the script multiple times and change the image names and id or can I just adjust the [code=html]var proll=['owen.gif','kit2rollover.jpg']//off/on [/code] or add another var proll=

thanks!!
Copy linkTweet thisAlerts:
@KorJan 21.2006 — It is not one of my brightest ideea (it is to be done more dynamic and short, ready to be used for any situations like that), but I sensed that you need it quickly... and maybe you could do it better starting from that point.
Copy linkTweet thisAlerts:
@artgrrlauthorJan 21.2006 — here is the site again. [URL=http://www.thescrapbooksite.com/default2.asp]www.thescrapbooksite.com[/URL]

This what I did....Remember I am a beginner here...

Each of the images on the site if you look at it ( February kit 1 resolutions, February kit 2 brights, February kit 3 Budding Blossom) all work as roll overs and change the owen.gif (large image)

Issue: As that owen.gif changes for each of the rollovers a different image should appear. So when you roll over february kit 1 resolutions, owen.gif changes to kit1rollover.gif. Then roll over february kit 2 brights, owen.gif changes to kit2rollover.gif. then roll over February kit 3 Budding Blossom, owen.gif changes to kit3rollover.gif

Right now the problem is that it only shows kit3rollover.gif. When you roll over any of them.

Here is the code I adjusted

[code=html]<script language="JavaScript" type="text/JavaScript">
<!-- Begin

var rel ='images/';//relative path of the pictures
var proll=['owen.gif','kit1rollover.jpg']//off/on
var proll=['owen.gif','kit2rollover.jpg']//off/on
var proll=['owen.gif','kit3rollover.jpg']//off/on
function roll(obj,id,ind){
var url=obj.getAttribute('src').split('/')[obj.getAttribute('src').split('/').length-1];
url=(url.indexOf('_off')>-1)?url.replace('_off','_on'):url.replace('_on','_off');
obj.setAttribute('src',rel+url);
document.getElementById(id).setAttribute('src',rel+ind);
}[/code]


Here is the code down in the html part of the page for each rollover:

[code=html]<a href="kit.asp" onmouseover="roll(this.firstChild,'pic2',proll[1])" onmouseout="roll(this.firstChild,'pic2',proll[0])"><img src="images/kitone_off.gif" alt="February Kit 1 Resolutions" width="140" height="140"></a>


<a href="kit.asp" onmouseover="roll(this.firstChild,'pic2',proll[1])" onmouseout="roll(this.firstChild,'pic2',proll[0])"><img src="images/kit3_off.gif" alt="February Kit 1 Resolutions" width="140" height="140"></a>

<a href="kit.asp" onmouseover="roll(this.firstChild,'pic2',proll[1])" onmouseout="roll(this.firstChild,'pic2',proll[0])"><img src="images/kit2_off.gif" alt="February Kit 2 Brights" width="140" height="140"></a> [/code]



Any ideas to help with this?
Copy linkTweet thisAlerts:
@artgrrlauthorJan 21.2006 — Here is what I did...for anyone out there looking to do multiple image swaps...a little different than the other posts out there....

Here are the changes that I had make the

var proll=

var proll2=

var proll3=

Then made the proper changes in the html code.....Yeah for me!!

Here is the corrected code that fixed it.

[code=html]<script language="JavaScript" type="text/JavaScript">
<!-- Begin

var rel ='images/';//relative path of the pictures
var proll=['owen.gif','kit1rollover.jpg']//off/on<br>
var proll2=['owen.gif','kit2rollover.jpg']//off/on<br>
var proll3=['owen.gif','kit3rollover.jpg']//off/on
function roll(obj,id,ind){
var url=obj.getAttribute('src').split('/')[obj.getAttribute('src').split('/').length-1];
url=(url.indexOf('_off')>-1)?url.replace('_off','_on'):url.replace('_on','_off');
obj.setAttribute('src',rel+url);
document.getElementById(id).setAttribute('src',rel+ind);
}[/code]


code down in html part of site-

[code=html]<a href="kit.asp" onmouseover="roll(this.firstChild,'pic2',proll[1])" onmouseout="roll(this.firstChild,'pic2',proll[0])"><img src="images/kitone_off.gif" alt="February Kit 1 Resolutions" width="140" height="140"></a>

<a href="kit.asp" onmouseover="roll(this.firstChild,'pic2',proll2[1])" onmouseout="roll(this.firstChild,'pic2',proll2[0])"><img src="images/kit2_off.gif" alt="February Kit 2 Brights" width="140" height="140"></a>

<a href="kit.asp" onmouseover="roll(this.firstChild,'pic2',proll3[1])" onmouseout="roll(this.firstChild,'pic2',proll3[0])"><img src="images/kit3_off.gif" alt="February Kit 1 Resolutions" width="140" height="140"></a>[/code]
Copy linkTweet thisAlerts:
@KorJan 22.2006 — I see that you have found a solution by yourself... Just a note

[B]//[/B][I]something[/I]

is a comment in javascript. That means it does not interfere with the code. I have put there some comments as explanation. Once you got the ideea, remove them (anyway you inserted there a HTML tag <br>, uselees, as there is a comment...

remove:

//off/on<br>

And...This is not the single way to code multiple rollovers, anyway
Copy linkTweet thisAlerts:
@artgrrlauthorJan 23.2006 — Yeah...I caught the <br> in there and pulled out before I saw your post....I left the //comments in the code so that the next poor fellow who is in the same shoes as me sees what *you* were showing me. I think that will also help them out.

And as a designer I realize there are million ways you can design a website just like there are a million ways you can program it.

:p
×

Success!

Help @artgrrl spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...