Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to revert to original css values after .css() method

I have an element I am changing with an animation as follows:

that$.animate({
                    opacity:'0.0'
                },300,
                function() {
                    $('#menuHolder').css({
                        left:'0px',
                        top:'0px',
                        height:'100%',
                        width:'100%',
                    },0);

This is meant to make menuHolder take up the whole screen. When clicking a separate button, I want menuHolder to revert to the original values i assigned in the style sheet. Here is the code for the return button:

$('.return').bind('click',
        function() {
            $(this).hide(300);

            tMT$ = $(this).parent();

            tMT$.animate({
                opacity:'0.0'
            },300,
            function() {
                $('#menuHolder').css({
                    left:$(this).style.left,
                    top:$(this).style.top,
                    height:$(this).style.height,
                    width:$(this).style.width
                },0);
            })

This doesnt work, because I have assigned the original css values with when that$.animate was executed. How should I assign the values of .return's click so that menuHolder reverts to its original css?

I don't want to manually reassign values. I would rather do it programmatically =D. Any help would be great.

Cheers.

like image 661
dopatraman Avatar asked Nov 26 '25 11:11

dopatraman


1 Answers

That is why you should not change individual CSS properties from your jQuery/Javascript code. Other than not being elegant, readable and maintainable and not separating presentation from behaviour, it will lead to situations like yours.

Instead:

Create classes in your CSS file:

.full-screen { left: 0; top: 0; width: 100%; height: 100%; }

and only use jQuery/Javascript to add/remove them:

$('#menuHolder').addClass('full-screen');
...
$('#menuHolder').removeClass('full-screen');

jQuery Class Manipulation Methods

like image 109
kapa Avatar answered Nov 28 '25 02:11

kapa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!