Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load images on Page load, CSS

I recently asked a question on using images as buttons using Java Script. the answer i got refereed me to using CSS instead and works fine. However The images i am using take about 0.2 seconds to load.

Before i used this code i had my page load all the images at the start and then a JS script to modify the zIndex of the images under certain mouse events, this however overrides the href links and failed to work.

With the new code all function work and so dose the href but each time a page loads only the first Image loads, the other 2 i am using under this image on load when the mouse events are triggered. This means that the buttons flash when an action is preformed. How can i modify this code to load the images at the same time the page loads ?

Code here: http://jsbin.com/casoy/12/edit?html,css,output

I thought i should show the old code as we..: http://jsbin.com/casoy/13/edit?html,css,js,output

like image 797
user3310078 Avatar asked Mar 14 '26 07:03

user3310078


1 Answers

The call to download the image will not occur until the css style is triggered.

There are a few ways to do this and organise the html to what you prefer,

-here is a crude example using your code to show the principle.

MARKUP

<div class="preload1"></div>
<div class="preload2"></div>

<a href="http://officialacescottie.co.uk/Home/Home.html">click me</a>

*CSS *

a {
  display: block;
  width: 600px;
  height: 114px;
  background-image:url("/Buttons/Home.gif");
  text-indent: -9999px;
  outline: 0;
}
a:hover, .preload1 {
  background-image:url("/Buttons/HomeP.gif");
}
a:active, .preload2 {
  background-image:url("/Buttons/HomeC.gif");
}
/* hide these off page in a way that 
   the browser will still call the background-image */
.preload2, .preload1 {
  position:absolute; left:-9999px; top:-9999px;
}

/* image urls are shortened here only so it presents better on SO */

This shows the basic principle that, when the a:hover is triggered, the background image referenced has already had a chance to 'preload' to the browsers cache.

like image 62
Rob Sedgwick Avatar answered Mar 16 '26 20:03

Rob Sedgwick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!