4 Ways To Preload Images With CSS or JavaScript

File-Adobe-Dreamweaver-CSS-01-icon 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;)

#preloadedImages {
       width: 0px;
       height: 0px;
       display: inline;
       background-image: url(path/to/image1.png);
       background-image: url(path/to/image2.png);
       background-image: url(path/to/image3.png);
       background-image: url(path/to/image4.png);
       background-image: url();
}

and below is another sample:

ul{
margin: 0;
padding: 0;
list-style-type: none;
font: 13px ‘Lucida Grande’, Arial, sans-serif;
}
ul li{
display: inline;
}
ul li a{
display:block;
width:120px;
text-decoration: none;
padding: 0.3em 1em;
color: #000;
background:url(images/link.gif);
}
ul li a:hover{
width:120px;
background:url(images/hover.gif);
}

OPTION 2: Preload The Images With JavaScript

<script type="text/javascript" language="JavaScript">
companyLogo = new Image();
companyLogo.src = "logo.gif";
</script>

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: 

$(document).ready(function(){
  $.preloadCssImages();
});

OPTION 4: Stick The Images In a Hidden Frame

A hidden frame is just a frame window set to 0%.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

Leave a Reply