If you use an image for the background image of an element on a mouseOver or :hover
event(for example, navigation menu which images are used), the image
will not load until the first :hover event if you only apply that
background-image in the CSS for the :hover state. The preloader
will help you with this! It’ll load those images when the page loads
and stores them in your browsers cache. So avoid delays with rollover
effects. In this article, you’ll learn 4 ways to preload images with CSS
or javascript technique.
OPTION 1: Use A Little CSS(display:none;)
5 | background-image : url (path/to/image 1 .png); |
6 | background-image : url (path/to/image 2 .png); |
7 | background-image : url (path/to/image 3 .png); |
8 | background-image : url (path/to/image 4 .png); |
9 | background-image : url (); |
and below is another sample:
5 | font : 13px ‘Lucida Grande’, Arial , sans-serif ; |
16 | background : url (images/link.gif); |
20 | background : url (images/hover.gif); |
OPTION 2: Preload The Images With JavaScript
1 | <script type= "text/javascript" language= "JavaScript" > |
2 | companyLogo = new Image(); |
3 | companyLogo.src = "logo.gif" ; |
You will need to have the two lines of JavaScript for each image you
want to preload, and you will need to browser has to support JavaScript
and have the two lines of JavaScript in your site.
OPTION 3: Automatically Preload images from CSS With jQuery
You can download a jQuery plugin from here, attached the jQuery javascript library and preloadCssImages.jQuery_v5.js to your page, and call $.preloadCssImages(); The source code like this:
1 | $(document).ready( function (){ |
OPTION 4: Stick The Images In a Hidden Frame
A hidden frame is just a frame window set to 0%.
|