views:

71

answers:

1

Hi there,

I've been searching for an answer for a long time with no avail, so it's time to ask. I'm trying to use .animate() to change the size of an image once clicked. The first state of the image would be a thumbnail size, which would animate to the image's original size, then to a thumbnail again.

edit: images load with 150px width by default.

The problem is, not all the images have the same "original" size so I need a way to tell one generic function to switch to the current image's original size.

This is my current code which loads on $(window).load:

$('img[class=resize]').click(function (){
        if ($(this).width() != 150)
        {
            $(this).animate({width: '150px'}, 400);
        }
        else
        {
            $(this).animate({width: ''}, 400);
        }
    });

Now this obviously doesn't work the way I want it, because .animate() thinks when I say width: '' I want to make the width = 0. Any suggestions how I should go about this?

A: 

Some basics in jQuery.

$('img[class=resize]') that kind of selector I call "bad karma". You should go with

$('img.resize')

to select images holding the class resize.

To your question, you should store the original image dimensions once. Maybe in an approach like this one:

$('img.resize').one('click', function(){
    var $this = $(this);

    if($.data($this, 'dimension') === null)
       $.data($this, 'dimension', {width: $this.width(), height: $this.height()});              

    $this.toggle(function(){
       $this.animate({width: '150px'}, 400);
    }, function(){
       $this.animate({width: $.data($this, 'dimension').width}, 400);
    }).click;
});

This is untested code, so you have to adapt some typos or logic maybe.

jAndy
This is wrong, you **need** `$(window).load()` here, there is a difference. Also `.data()` won't return undefined since jQuery 1.4, it's null so a simple `!$.data()` will do.
Nick Craver
fixed that, doesn't `ready()` use onload for the window? at least for IE's ? I think so..
jAndy
@jAndy - No, `document.ready` fires **before** images are loaded, `window.load` fires **after**, there are other differences, but the main one is all that matters for this question.
Nick Craver
@Nick: Ok I just trust you in that. Removed that part, but I'm still wondering, jQuery source: `window.attachEvent( "onload", jQuery.ready );`
jAndy
I used window.load because in another project Google Chrome was picky about it when dealing with images. Anyway, the images load with 150px width by default, so with the above code it seems to me I'm just setting 150px for the original width.
romeozor
My example code actually should work for all circumstances.
jAndy
Well you can see for your self http://romeozor.com/Pre-E3.htmlThe bottom 2 images are the point of interest. The smaller one loads with 150px width and the larger one without any width. It does work with the larger one, but since the code is getting a width that was already "poisoned" with the smaller one, it thinks the original width is 150px.
romeozor
@jAndy - Had some things to attend to here, sorry for the delay. `window.onload` is a fallback only mechanism "just-in-case", you can see the comments and the preferred mechanisms in `bindReady` here: http://github.com/jquery/jquery/blob/master/src/core.js#L393
Nick Craver