Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pre-Loading Images for an Image Button Rollover

Status
Not open for further replies.

JabbaTheNut

Programmer
Joined
Jul 29, 2002
Messages
176
Location
US
Can someone provide some code examples for pre-loading images for an Image Button rollover (i.e., "onmouseover", "onmouseout", etc.). I have tried following code unsuccessfully:

This is the javascript I am currently using...

<script language=&quot;javascript>

// Pre-load the rollover images

function pgInit ()
{
---image1 = New Image();
---image2 = New Image();

---image1.src = &quot;images/myimage_over.gif&quot;
---image2.src = &quot;images/myimage_out.gif&quot;
}

// Provide image to use, given the state

function rollImage (state)
{
---switch(state)
---{
------case &quot;over&quot;:
---------image1.src;
------case &quot;out&quot;:
---------image2.src;
---}
}

</script>

I have also included the following in my VB code-behind file under the Page_Load procedure to add the &quot;onmouseover&quot; and &quot;onmouseout&quot; events to the Image Button control on the rendered page...

myImgBtn.Attributes.Add(&quot;onmouseover&quot;, &quot;this.src=rollImage('over');&quot;)
myImgBtn.Attributes.Add(&quot;onmouseout&quot;, &quot;this.src=rollImage('out');&quot;)

The above code does not work. Can someone either correct my above code, tell me that the code appears correct and the problem must be something else, or suggest a different method? I want to pre-load images and use them for an image rollover for an Image Button.

Thanks in advance :) Game Over, Man!
 
I have a mouseover working on the following page (a frog):


The code is as follows:

In the upper part of the HTML code:

<HEAD>
<title>Main Data Menu</title>
<Script type=&quot;text/JavaScript&quot;>
<!-- Hide Script from non-Javascript browsers
var pre=new Image(); pre.src=&quot;images/frogdance.gif&quot;;
function rollover(n)
{
document.getElementById(&quot;a&quot;).src=(n!=0)?&quot;images/frogdance.gif&quot;: &quot;images/frogdotb.gif&quot;;
}
//-->
</Script>
<style type=&quot;text/css&quot;>
img {border-width:0}
//-->
</style>
</HEAD>

...and where the image is located:

<a href=&quot;javascript://&quot; onmouseover=&quot;rollover(1)&quot; onmouseout=&quot;rollover(0)&quot;>
<img name=&quot;a&quot; id=&quot;a&quot; src=&quot;images/frogdotb.gif&quot; border=&quot;0&quot;>

..and it works.
 
This is what I use.


Code:
<script language=&quot;JavaScript&quot;>
<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

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(&quot;#&quot;)!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf(&quot;?&quot;))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

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>
</head>

<body onLoad=&quot;MM_preloadImages('images/bold.gif','images/bold_over.gif','images/bullets.gif','images/bullets_over.gif')&quot;>

	<a href=&quot;#&quot; onMouseOut=&quot;MM_swapImgRestore()&quot; onMouseOver=&quot;MM_swapImage('Image0','','images/bullets_over.gif',1)&quot;>
		<img name=&quot;Image0&quot; border=&quot;0&quot; src=&quot;images/bullets.gif&quot;>
	</a>


	<a href=&quot;#&quot; onMouseOut=&quot;MM_swapImgRestore()&quot; onMouseOver=&quot;MM_swapImage('Image1','','images/bold_over.gif',1)&quot;>
		<img name=&quot;Image1&quot; border=&quot;0&quot; src=&quot;images/bold.gif&quot; height=&quot;28&quot; alt=&quot;contact&quot;>
	</a>

</body>
________________________________________________________________________
Are you trying to debug your ASP applications? See faq333-3255 for more details

regards,
Brian
 
Thanks for your help. It works now :)

I solved the problem with my original example. My code was poorly written. Here is the proper layout.

In the <script></script> tag of the .aspx page enter the following...

<script language=&quot;javascript&quot;>
// Pre-Load Rollover Images
// Insert call to function in <body> tag
function pgInit ()
{
image1 = New Image();
image2 = New Image();
image1.src = &quot;images/myimage_over.gif&quot;;
image2.src = &quot;images/myimage_out.gif&quot;;
}

// Use this function to set image on rollover
function rollImage (state)
{
switch(state)
{
case &quot;over&quot;:
document.getElementById(&quot;myImgBtn&quot;).src = image1.src;
break;
case &quot;out&quot;:
document.getElementById(&quot;myImgBtn&quot;).src = image2.src;
break;
}
}

</script>

In the VB code-behind file under the Page_Load procedure, enter the following...

myImgBtn.Attributes.Add(&quot;onmouseover&quot;, &quot;javaScript:rollImage('over');&quot;)
myImgBtn.Attributes.Add(&quot;onmouseout&quot;, &quot;javaScript:rollImage('out');&quot;)
Game Over, Man!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top