views:

565

answers:

4

Hello,

I have an image on a webpage and when the user hovers over it, another image appears. When then hovering over the appearing image, it flickers.

Anybody any idea why that is?

Tony

UPDATE: The first image does not dissapear when hovering, just another (smaller) image appears over the top in the left top corner. When now moving over that smaller image, then the flicker appears.

The image on the site is part of a gallery, so it's a php variable and gets loaded when a user selects from a list of images. So embedding one into the other is very hard.

+3  A: 

Because the browser is fetching the new image. The best solution is to incorporate both images into one, and either purely use CSS to change the background-position on :hover, or ( for IE6 and non-anchor elements ) change the background position with JS.

meder
See: CSS spritemap. Good article here: http://www.alistapart.com/articles/sprites
Mike Robinson
The image is created dynamically with Javascript (createElement). It is hidden at first and then shown when hover over some existing image. But then hovering over the new image causes the flicker
Tony
Ok. My explanation still applies.
meder
+1  A: 

In IE? IE is notorious for not caching images that are loaded dynamically (either with CSS :hover or due to Javascript events). You can use CSS sprites (basically, using one image file to display both images, and using positioning to show one or the other; tutorial, linked by Mike Robinson), or you can use image preloading:

var preloadImg = document.createElement('img');
preloadImg.src = 'path/to/image';

Edit: and other browsers will do the same on first load. IE just may continue to do it on every switch.

eyelidlessness
A: 

I guess you're probably using IE. This is a bug with the way it implements caching in some versions. You fix it by either configuring your web server to send proper cache headers, or by using CSS sprites. I recommend the latter, as that means less HTTP requests, and the preloading works even without JS.

Reinis I.
A: 

If I understand you right, you probably replace the src of your image every time the mouseover event is fired. So, even when your image is replaced, your event is fired and the image is replaced with itself, which may cause flickering.

wildcard