0

I have a menu that slides out on clicking the nav-toggle class.

Now I also want the nav-toggle class (the menu icon) to move along 226px from the right, so it moves at the same time as the #navigation menu. Then if clicked again it will collapse the menu (as it currently does) and go back to right:0 position.

See my commenting out in the third last line

jQuery('.nav-toggle').click(function () {
        var $marginLefty = jQuery('#navigation');
        $marginLefty.animate({
            right: parseInt($marginLefty.css('right'), 10) == 0 ? -$marginLefty.outerWidth() : 0
        });
        jQuery('.nav-toggle').animate({
            right: "226px" 
        // How do I make it so if it is at 226px when clicked
        // it should then go to "right: 0", that toggle effect as above
        });
    });
3
  • Can you make a fiddle? Commented Sep 27, 2013 at 5:55
  • Could you include the relevant html aswell? :) EDIT: or a fiddle.. that'll work too.. ;) Commented Sep 27, 2013 at 5:56
  • Can you create and post a jsFiddle with your html/css/js Commented Sep 27, 2013 at 5:57

3 Answers 3

4

Probably just like you did the #navigation one:

 jQuery('.nav-toggle').animate({
    right: parseInt(jQuery('.nav_toggle').css('right'), 10) == 0 ? "226px" : 0;
 });
Sign up to request clarification or add additional context in comments.

5 Comments

Much cleaner than what I was going to put. +1
The page isn't loading now :) I'll paste the code here.. jQuery('.nav-toggle, .nav-close').click(function () { var $marginLefty = jQuery('#navigation'); $marginLefty.animate({ right: parseInt($marginLefty.css('right'), 10) == 0 ? -$marginLefty.outerWidth() : 0 }); jQuery('.nav-toggle').animate({ right: parseInt(jQuery('.nav-toggle').css('right'), 10) == 0 ? "226px" : 0; }); });
My fault, sorry an extra ;
No problem. Glad you found it.
Thanks @mavrosxristoforos, much apprciated!
0

Just add a class to check if the nav is expanded/moved or not.

var navbar = $('.nav-toggle');    
if (navbar.hasClass('expanded'))
            {
                $(navbar).animate({'right':'226px'}).removeClass('expanded');
            } else {
                $(navbar).animate({'right':'0px'}).addClass('expanded');
            }

Note these numbers may need flipping.

1 Comment

Try again possibly, and try flipping the 226px and the 0px; or make it 'marginLeft'. Otherwise i'm not sure sorry.
0

I think you need to do something like this:

jQuery('.nav-toggle').click(function () {
    var $marginLefty = jQuery('#navigation');
    $marginLefty.animate({
        right: parseInt($marginLefty.css('right'), 10) == 0 ? -$marginLefty.outerWidth() : 0
    });
    if ($('.nav-toggle').css('right') == '0px') {
        $(".nav-toggle").stop().animate({right: '226px'}, 1000);
    } else {
        $(".nav-toggle").stop().animate({right: '0px'}, 1000);
    };
});

1 Comment

Thanks. The thing is, I want to encapsulate that within the on click function. So if the user clicks on nav-toggle it will not only bring in/out the nav div, but also animate the nav-toggle position. I can't use a button toggle because I'm already using an on click and want to integrate with that.

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.