Simple Image Preloading

General Discussion about Mootools 1.2

Moderator: 1.2 Moderators

Simple Image Preloading

Postby silentsnake on Fri Jan 08, 2010 1:32 am

Hello everyone,

I'am working on a script which 'popups' images as an overlay. Nothing special, nothing new - just simple stuff.
But i stuck with finding a appropriate way to preload the image.

The image object itself and the div container around stays the same way all the time (internally). It will just be shown or hidden, or repositioned by scroll or resize events.

So if someone clicks on a thumbnail, he calls a function that is fading the background, changing the url of the image object and then displays (and position) the container. Problem is - without having the picture completely loaded, positioning will fail.

Therefore I need some kind of event which will wait until the image object (after changing the url) is fully loaded and then calls the 'show' function. I've tried many different approaches, using JS internal onload Event, addEvent from MooTools Core, Asset.image from MooTools More, and so on. Nothing has really worked. So don't get me wrong, i don't want to load a bunch of images, I just want to ensure that a specific image object is loaded before it will be shown.


Thanks!
silentsnake
 
Posts: 5
Joined: Fri Jan 08, 2010 1:15 am

Re: Simple Image Preloading

Postby wowenkho on Fri Jan 08, 2010 2:01 am

Asset.image should've worked for this kind of endeavor. though if it failed, then it might be your implementation of Asset.image itself. how did you make your Asset.image code?
my sword is yours...
http://wowenkho.com

for those who want to talk with me quicker, add me on Yahoo! Messenger through my ID: michaelapeles
User avatar
wowenkho
mootools freak
 
Posts: 1146
Joined: Sat Aug 15, 2009 10:38 am
Location: Antipolo City, Philippines

Re: Simple Image Preloading

Postby silentsnake on Fri Jan 08, 2010 4:12 am

I'm generating the image object like that: (this.src is the source file)
  1. var imgelem = new Element('img', { 'src': this.src });
  2. imgelem.inject($(this.id));


preloading: (show() should be called when the image is loaded)
  1. var myImage = new Asset.image(this.src, { onload: show() });


changing image:
  1. this.src = newsrc;
  2. $$('div#' + this.id + ' img')[0].setProperty('src', newsrc);


I also tried using the SAME actual object, so instead of generating and injecting the image i just injected 'myImage', without success.

show() is called, but the loading is not finished at that point.

I'm using this function for getting the size of the 'popup' by the way - in text based context it works quiet well:
  1. this.size =  $(this.id).measure(function() {
  2. return this.getSize();
  3. });



Thanks.
silentsnake
 
Posts: 5
Joined: Fri Jan 08, 2010 1:15 am

Re: Simple Image Preloading

Postby saurabh1985 on Mon Jan 18, 2010 3:14 pm

i should have checked the "more" docs.it's like that...
saurabh1985
 
Posts: 3
Joined: Mon Jan 18, 2010 3:08 pm


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest