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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
Thanks! This reset my 'top' to the CSS-stylesheet-defined value when 'auto' was not what I wanted!
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.... :/
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 );

});

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.