cssimagebackgroundpreloaderpreload

How to preload CSS images?


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>
$(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 solving this issue.


Solution

  • 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

    <script>
        var pic = new Image();
        var pic2 = new Image();
        var pic3 = new Image();
        pic.src="images/inputs/input1.png";
        pic2.src="images/inputs/input2.png";
        pic3.src="images/inputs/input3.png";
    </script>