1

One that's been bugging me for a while. How would I combine these three sets of code together?

$(".fly-out-menu").toggle(
      function () {
        $('#page').animate({left: 250}, 'fast'); },
      function () {
        $('#page').animate({left: 0}, 'fast'); }
);
$(".fly-out-menu").toggle(
      function () {
        $('.sticky-container').animate({left: 250}, 'fast'); },
      function () {
        $('.sticky-container').animate({left: 0}, 'fast'); }
);
$(".fly-out-menu").toggle(
      function () {
        $('body').css('position','fixed') },
      function () {
        $('body').css('position','static') }
);

Thanks, R

6
  • 2
    toggle method is deprecated, there is no rule that prevents you from wrting more-than-one-line functions. Commented Dec 14, 2012 at 23:38
  • Doesn't say it's deprecated... api.jquery.com/toggle Commented Dec 14, 2012 at 23:41
  • 1
    You are using this toggle api.jquery.com/toggle-event Commented Dec 14, 2012 at 23:42
  • Is there an alternative? Commented Dec 14, 2012 at 23:44
  • 1
    See the edit I just made. (Thank you @undefined for bringing this up) Commented Dec 14, 2012 at 23:54

2 Answers 2

5

Like that?

$(".fly-out-menu").toggle(
    function(){
        $('#page, .sticky-container').animate({left: 250}, 'fast');
        $('body').css('position','fixed');
    },
    function(){
        $('#page, .sticky-container').animate({left: 0}, 'fast');
        $('body').css('position','static');
    }
);

As pointed out by @undefined, this event is now deprecated.
One way of handling this yourself would be:

$(".fly-out-menu").on("click", function(){
    var t = $(this);
    if(!t.data("alreadyclicked")){
        t.data("alreadyclicked", true);
        $('#page, .sticky-container').animate({left: 250}, 'fast');
        $('body').css('position','fixed');
    } else {
        t.data("alreadyclicked", false);
        $('#page, .sticky-container').animate({left: 0}, 'fast');
        $('body').css('position','static');
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for all your help. Seems odd that they would deprecate this, but hey ho.
1

You mean like this?

$(".fly-out-menu").toggle(
function () {
    $('#page').animate({left: 250}, 'fast'); 
    $('.sticky-container').animate({left: 250}, 'fast');
    $('body').css('position','fixed');
},
function () {
    $('#page').animate({left: 0}, 'fast');
    $('.sticky-container').animate({left: 0}, 'fast');
    $('body').css('position','static');
}
);

Or like this:

$(".fly-out-menu").toggle(
function () {
    $('#page, .sticky-container').animate({left: 250}, 'fast');
    $('body').css('position','fixed');
},
function () {
    $('#page, .sticky-container').animate({left: 0}, 'fast');
    $('body').css('position','static');
}
);

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.