2

I'm having trouble getting the "else" bit of this working. Anyone know what the problem is?

var navOpen = false;
                if (navOpen == false) {
                    $("nav").click(function() {
                        $(this).css("bottom","0");
                        navOpen = true;
                    });
                } else {
                    $("nav").click(function() {
                        $(this).css("bottom","-84");
                        navOpen = false;
                    });
                }
2
  • Where is the rest of the code? yo are setting navOpen to false just before the if, so it will never evaluate the else bit. Commented Feb 27, 2013 at 3:42
  • You need to specify 'px' or '%' to the -84. Commented Feb 27, 2013 at 3:43

4 Answers 4

6

The condition is in the wrong place.

 var navOpen = false;
 $("nav").click(function() {
      if (navOpen == false) {
           $(this).css("bottom","0");
           navOpen = true;
      } else {
           $(this).css("bottom","-84px");
           navOpen = false;
      }
 });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that worked perfectly. Shouldn't code late at night, things go over my head lol. I'll accept when it'll let me in a few minutes.
5

You are binding several handlers to the same element, you can use css method:

$("nav").click(function() {
   $(this).css("bottom", function(i, bottom) {
      return bottom === '0px' ? '-84px' : '0px';
     // return navOpen ? '-84px' : '0px';
   });
})

2 Comments

Beat me to it. You were missing the unit (px) and binding multiple time (super bad).
He might need to do more with the navOpen variable, but this is an elegant solution +1
4

Try with

$(this).css("bottom","-84px");

Comments

4

You need to define metering (e.g px, %). CSS doesn't support just numering parameters like HTML attribute does.

1 Comment

Wow, can't believe I didn't try that. Thanks. I'm accpeting another answer because it fixed another issue I was having, but you get an upvote.

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.