23

I've got...

$("#menu_tl").click(function() {

    $('.page').fadeOut();
    $('.page').animate({
        "width":  "0px", 
        "height": "0px", 
        "top":    "50px", 
        "left":   "50px"
    });

    $('#page1').fadeIn();
    $('#page1').animate({
        "width":  "500px", 
        "height": "400px", 
        "top":    "-350px", 
        "left":   "-450px"
    }, 2000);
});

$("#menu_tr").click(function() {

    $('.page').fadeOut();
    $('.page').animate({
        "width":  "0px", 
        "height": "0px", 
        "top":    "50px", 
        "left":   "50px"
    });

    $('#page2').fadeIn();
    $('#page2').animate({
        "width":  "500px", 
        "height": "400px"
    }, 2000);
});

But I want to say that when the second function is clicked all altered css by the first one should be reset. This means that the following line is wrong:

$('.page').animate({
    "width":  "0px", 
    "height": "0px", 
    "top":    "50px", 
    "left":   "50px"
});

is wrong and should be replaced by a global reset. Hope this is clear enough...

2 Answers 2

55

This

$('.page').css({"width":"", "height":"", "top": "", "left" : ""});

should do the magic for you.

3
  • yup, it does! Thank you very much! But is there a way to do it more gradually? It now just disapears in the blink of an eye.
    – Aeonius
    Apr 30, 2011 at 21:33
  • Thanks! This reset my 'top' to the CSS-stylesheet-defined value when 'auto' was not what I wanted!
    – Spork
    Feb 7, 2013 at 11:49
  • it works when using .css() but it doesn't work when using .animate(), when using .animate() it simply changing the style to width: 0px; height: 0px; which caused my <div> to disappear.... :/
    – Ricky Levi
    Sep 18, 2016 at 19:16
3

Store the initial css values you will be changing in a variable:

var $page = $('.page'),
    initialCss = {
        width:  $page.css('width'),
        height: $page.css('height'),

        // Could also be .css('top'), .css(left')
        // depends on your initial stylesheet
        top:    $page.position().top + 'px',
        left:   $page.position().left + 'px'
    };

$("#menu_tr").click(function(){

    $page.animate( initialCss, 2000 );

});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.