On a mouse click of a button, I switch out an image in a div with another partial in Rails. The problem is that loading the image in that partial is slow… I want to preload the image in it. Is there a way to do this? Can I preload the partial somehow?
in my view
-@other_images.each do |img|
.thumbnail_wrapper
.thumbnail_image
=image_tag img.image.url(:thumbnail), :class => 'button' , :onClick => "replaceImg(#{img.id});"
js
function replaceImg(id){
var url = '/images/refresh/' + id;
new Ajax.Updater('content_image', url);
}
Many thanks!
I suppose your partial are renderd withint the view. Preloading is cheating the browser to make it sure it needs a resource, and it needs it now.
Javascript
You can force the images to preload before being actually shown in Javascript:
In this way for every iteration within the
forloop one of your images are loaded.To programmatically generate the array you can (in erb):
Just HTML
Now images are (pre)loaded in parallel according to the browser and server setup.
CSS
Set the image you want to preload as the background image of a div noone will ever see:
Iterate for every image you want to preload.