Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old September 1st, 2008, 08:50 AM
Member
 
Join Date: Jun 2008
Posts: 36
Default What is the better way to preload images?

Hi
i have the used the below code to switch the button image on mouseover
and mouseout.
Code:
<html:button property="Button" value="Display" styleClass="displaybutton" 
	onmouseover="this.className='displaybutton displaybuttonover'"
	onmouseout="this.className= 'displaybutton'"/>
CSS definition of displaybutton and displaybuttonover is shown below.
Code:
.displaybutton {
  background: transparent url("/images/btnDisplay.gif") no-repeat;
  width:64px;
  padding: 22px 0 0 0;
  overflow: hidden;
  height:17px; 
  border:0; 
  cursor: pointer; /* hand-shaped cursor */
  cursor: hand; /* for IE 5.x */
}

.displaybuttonover {
  background: transparent url("/images/btnDisplay_over.gif") no-repeat;
}

but it produced delay in switching the image on mouseover in IE browser alone.

To avoid this delay i thought of using the below code on load to preload the image.

Code:
function imagePreloader()
{

objImage = new Image();     

var buttons=new Array('btnDisplay_over.gif');
 
 for(i=0;i<buttons.length;i++)
{
objImage.src='/images/'+buttons[i];

}
}
but i belive this is not good solution, Is there any other effective way to Preload images, because similar to display button, i have large number of buttons in my site, so preloading all of them with imagePreloader() function might increase the delay in loading the page. Can any one suggest me a better solution?

Thanks in Advance !!!!
Kindly Notify me if my question is unclear .
Reply
  #2  
Old September 2nd, 2008, 02:01 AM
Familiar Sight
 
Join Date: Feb 2007
Posts: 205
Default

If you start the preload operation using the onload event there is no delay in loading the rest of the content, although there will be some delay before the preloaded images become available.

Your code will preload only the last image, since you're reassigning source files to the same Image object.

This is the simplest fix for what you have already:
Code:
function imagePreloader()
{
 this.objImage = [];
 
 var buttons=new Array('btnDisplay_over.gif');
 
 for(var i=0;i<buttons.length;i++)
 {
  this.objImage[i] = new Image();     
  this.objImage[i].src='/images/'+buttons[i]; 
 }
}
Reply
  #3  
Old September 2nd, 2008, 06:59 AM
Dormilich's Avatar
Expert
 
Join Date: Aug 2008
Location: Leipzig, Germany
Age: 31
Posts: 599
Default

alternatively, you could have a look at the css pseudo class :hover and how you can apply a 'mouseover event' this way (desribed in this article at ALA)
Reply
  #4  
Old September 18th, 2008, 03:46 PM
Member
 
Join Date: Jun 2008
Posts: 36
Default

Thank you for the reply. Will definitly make use of them and reply back
Reply
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles