views:

1782

answers:

5

Hello guys,

I have a hidden contact form which is deployed clicking on a button. Its fields are set as CSS background images, and they always appears a bit later than the div that have been toggled.

I was using this snippet in the <head> section, but with no luck (after I cleared the cache) :

<script type="text/javascript">
$(document).ready(function() {
     pic = new Image();
     pic2 = new Image();
     pic3 = new Image();
     pic.src="<?php bloginfo('template_directory'); ?>/images/inputs/input1.png";
     pic2.src="<?php bloginfo('template_directory'); ?>/images/inputs/input2.png";
     pic3.src="<?php bloginfo('template_directory'); ?>/images/inputs/input3.png";
});
</script>

I'm using jQuery as my library, and it would be cool if I could use it as well for arranging this issue.

Thanks for your thoughs.

A: 

If the page elements and their background images are already in the DOM (i.e. you are not creating/changing them dynamically), then their background images will already be loaded. At that point, you may want to look at compression methods :)

cpharmston
mmmm... good point cpharmston, and this explains why the issue persists even with the images cached. So, that does means there's no workaraound ?
Peanuts
Create smaller images! JPEGs are easily compressed, there are command line tools for reducing the size of PNGs (http://pmt.sourceforge.net/pngcrush), and you can reduce the number of colors in the image to reduce the size of GIFs. Of course, you can also spring for faster internet service ;)
cpharmston
+3  A: 

try with this:

var c=new Image("Path to the background image");
c.onload=function(){
   //render the form
}

With this code you preload the background image and render the form when it's loaded

mck89
mck89, thanks this looks pretty good. But, should I use this code inside the 'head' or inline ? I mean, must I fill it with html ?
Peanuts
You must use it in the head. I think that you can use it inside the $(document).ready
mck89
A: 

how about loading that background image somewhere hidden. That way it will be loaded when the page is opened and wont take any time once the form is created using ajax:

body {
background: #ffffff url('img_tree.png') no-repeat -100px -100px;
}
Santi
A: 

Hi guys, I can confirm that my original code seems to work. I was casually sticking to an image with a wrong path.

Here's a test : http://paragraphe.org/slidetoggletest/test.html

Peanuts
+2  A: 

If you're reusing these bg images anywhere else on your site for form inputs, you probably want to use an image sprite. That way you can centrally manage your images (instead of having pic1, pic2, pic3, etc...).

Sprites are generally faster for the client, since they are only requesting one (albeit slightly larger) file from the server instead of multiple files. See SO article for more benefits:

http://stackoverflow.com/questions/1289372/css-image-sprites

Then again, this might not be helpful at all if you're just using these for one form and you really only want to load them if the user requests the contact form...might make sense though.

http://www.alistapart.com/articles/sprites

ajhit406
Yes, CSS Sprites are the way to go. Just make sure that one of the images in that sprite appears before the hidden form is launched (like on page load).
Steve