0

I'm trying to animate a panel to come in and out from the top of the page. Unfortunately my if statement is not working. Here's my code:

var panelShown = false;
$(document).ready(function(){
    $('#nav_arrow').click(function(){
        panelShown = true;
        $('#nav_panel').animate({top: '0px'}, 1000, function(){});
        $('#nav_bar');
    });
    if(panelShown == true){//originally written as if(panelShown){
        $(document).click(function(){
            panelShown = false;
            $('#nav_panel').animate({top: '-200px'}, 1000, function(){});
        });
    }
});

The first part works fine, the panel animates down. But I can never get into the if statement. Any help would be greatly appreciated.

4
  • 1
    Damn, I'm an idiot. The if statement needs to be inside the click listener. :/ Commented Feb 28, 2013 at 11:20
  • Stepping through your code (i.e. debugging) would have made this immediately apparent. Commented Feb 28, 2013 at 11:21
  • It looks like StackOverflow is now a hi-tech version of Rubber Duck debugger ;-) Commented Feb 28, 2013 at 11:29
  • I'm using a notepad, I don't have much debugging. I tried using console.log, but I just missed that it was only running once. Commented Feb 28, 2013 at 11:31

1 Answer 1

1

It really whould never be called because on ready it will be false.

Try this:

var panelShown = false;
$(document).ready(function(){
    $('#nav_arrow').click(function(){  
        $('#nav_panel').animate({top: (panelShown ?'-200px' : '0px')}, 1000, function(){});
        panelShown = !panelShown;
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, just realised that there, thanks :) 9 minutes until I can accept your answer.

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.