Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell when a CSS background image has loaded? Is an event fired? [duplicate]

Tags:

I have a sidebar widget that has an image background.

Over this is a search input form. I don't want the input to show before the image has loaded.

Is there a way to attach an load event handler to CSS background images like normal img elements/objects?

I know this could be done on a normal image, but I'd like to keep it as a CSS background because the image is part of a sprite. I am using jQuery, so solutions using jQuery or plain DOM JS are equally good.

like image 310
Cole Avatar asked Jun 08 '11 22:06

Cole


People also ask

What defines if a background image will be repeated?

The background-repeat property sets if/how a background image will be repeated. By default, a background-image is repeated both vertically and horizontally. Tip: The background image is placed according to the background-position property.

Which property sets how a background image will be repeated?

The background-repeat CSS property sets how background images are repeated. A background image can be repeated along the horizontal and vertical axes, or not repeated at all.

Which background property specifies whether the background image is repeated or not?

Description. The background-repeat CSS property specifies whether the background-image is repeated or tiled after it has been sized and positioned, and how. It is often more convenient to use the shorthand background property.

Which event occurs when page or image is loaded?

onload: The onload event is triggered when an image is loaded and is executed.


1 Answers

You could load the same image using the DOM / a hidden image and bind to the load event on that. The browser's caching should take care of not loading the image twice, and if the image is already loaded the event should fire immediately... not tested, tough.

like image 105
Félix Saparelli Avatar answered Oct 02 '22 19:10

Félix Saparelli