views:

423

answers:

3

i have a bunch of images that are positioned absolutely, i want to be able to click a button and have them all animate to where they would normally be on the page if they had the position: relative.

so is it even possible to animate from position:absolute to position:relative in jquery?

this is what i have:

$(".change_layout").click(function(){

    $(".book_img").animate({position:'relative', top:'0px', left:'0px'}, 1000)

})
A: 

I don't think you can do that. jQuery animate only works on any "numeric CSS property".

rosscj2533
Not really... you can change the color of text or background too. :)
TiuTalk
but isn't the color a numeric value?
ExodusNicholas
@TiuTalk - good point, but I think ExodusNicholas is right, in this situation jQuery would use the numeric aspects of the color to animate it, incrementing/decrementing the RGB values as appropriate to shift the color.
rosscj2533
if you watch the code when jquery is animating a color, you'll see the RGB values placed into the elements style attribute and increasing/decreasing(like rosscj2533 said).
ExodusNicholas
A: 

No.

However, you could check the element's current location (call .position()), set the position to relative, and animate from the original location to the current one.

SLaks
i'm not sure how i would go about this, is there a chance you could help me get started? what i'm trying to do is provide 2 different views for people, each article has an image, and i want those images to be able to animate their position when the 2nd view button is pressed, and then when the 1st view button is pressed, have them animate back to the first position.
ExodusNicholas
For an example, see Tatu Ulmanen's answer, which is exactly what I was describing.
SLaks
+3  A: 

No, you cannot animate it directly but you can find out the end point and animate the position there. Something like this might work when animation to the static position:

$('img.foo').each(function() {

    var el = $(this);

    // Make it static
    el.css({
        visibility: 'hidden', // Hide it so the position change isn't visible
        position: 'static'
    });

    // Get the static position
    var end = el.position();

    // Turn it back to absolute
    el.css({
        visibility: 'visible', // Show it
        position: 'absolute'
    }).animate({ // Animate to the static position
        top: end.top,
        left: end.left
    }, function() { // Make it static
        $(this).css('position', 'static');
    });
});

It's a bit hacky, but it should work.

Tatu Ulmanen
You should call `position()`, not `offset()`.
SLaks
ah you are wonderful. the only problem is that all of the images(they're spread out across the page) animate to the position of the first image, and then they instantly appear in their static position, but each image doesn't animate to it's own static position. any clue how to fix this?
ExodusNicholas
Do an `each` loop where you handle each of the images individually. You are probably now selecting all the elements in one declaration (your `var el` is something like `$('img.foo')`, this makes all operations relative to the first image. I've updated my post to show this.
Tatu Ulmanen
@SLaks, good point, I have actually always used `offset()` but `position()` does seem a bit more appropriate.
Tatu Ulmanen
now the first image animates to where it should, but all of the other images animate to the absolute position of where the first image was.
ExodusNicholas
Sorry, my mistake. It should have been `el.position()` instead of `$('element').position()`. Fixed.
Tatu Ulmanen
now it's back to all the images animating to the first images static position :(
ExodusNicholas
@ExodusNicholas, perhaps I should have tested it myself before posting :) I'll take a look at it at some point.
Tatu Ulmanen