0

What's the best way to switch images on mouse-over events ?

I want to swap 2 images when I move the mouse over and out an object on my webpage.

I would like to know for both cases: - css-background images assigned to , , elements (image url is in css file) - elements (image url is in html code)

I'm currently using jQuery for it. But the problem is that the second image is always showed with a bit of delay the first time a roll-over event occurs, because it needs to be loaded.

NB. I need a solution compatible with all browsers! (IE 6-7 not required. IE 8, Firefox, Safari, Chrome...)

thanks

aneuryzm
  • 63,052
  • 100
  • 273
  • 488
  • Seems to me a duplicate of: http://stackoverflow.com/questions/4677249/javascript-hover-image-replacement – lepe Jan 14 '11 at 09:33

4 Answers4

4

Use CSS sprites.

Because "both" images are downloads at once - they will appear instantly for the user.

m.edmondson
  • 30,382
  • 27
  • 123
  • 206
0

You can use CSS sprite in this situation.

You can also read What are the advantages of using CSS sprites?

Community
  • 1
  • 1
rahul
  • 184,426
  • 49
  • 232
  • 263
0

Define two class like (clsImg1,clsImg2) and define images to their backgrounds. You should change the classNames while mouseover-mouseout events called

0

You can try to load the image with jQuery before showing this way:

$('img').attr('src', 'YOUR_IMAGE_PATH').hide().appendTo('html');