user934902
user934902

Reputation: 1204

jquery slidetoggle two seperate elements with 1 click event

Trying to slide toggle two separate elements on 1 click event. Got it somewhat working however the one element is not animated and I'm having issues making the animation smooth

CSS

.wrapper {
    width: 300px; margin: 10px auto; height: 300px;
    background-color: #e5e5e5; 
    border: 1px solid #d8d8d8;
    position: relative;
}

.menu { 
    list-style: none; margin: 0; padding: 0; width: 62%; background-color: blue;
    position: absolute; right: 0; top: 0; height: 100%; display: none; z-index: 300;
}

a { 
    display: block; width: 50px; height: 60px; background-color: orange; 
    position: absolute; right: 5%; z-index: 500;
}

.moveButton { right: auto; left: 38%; }

HTML

<div class="wrapper">
<a href="#">Menu</a>
<ul class="menu">
    <li>Item</li>
    <li>Item</li>
    <li>Item</li>
    <li>Item</li>
    <li>Item</li>
</ul>
</div>

jquery

$('a').click(function() { var effect = 'slide'; var options = { direction: 'right' } var duration = 200;

$('.menu').stop().toggle(effect, options, duration);

$(this).toggleClass('moveButton'); });

Fiddle http://jsfiddle.net/NZ2DX/

NOTES Ive tried animating the toggleClass but it comes in from the left instead of the right

Menu button should be inside menu on expansion

enter image description here

Upvotes: 0

Views: 605

Answers (1)

Bram Vanroy
Bram Vanroy

Reputation: 28437

I am guessing you want this.

$('a').click(function () {
    var effect = 'slide';
    var options = {
        direction: 'right'
    }
    var duration = 200;
    if ($('.menu').is(":hidden")) {
        $(this).animate({
            right: "62%"
        }, duration);
    } else {
        $(this).animate({
            right: "5%"
        }, duration);
    }
    $('.menu').stop().toggle(effect, options, duration);
});

If you really want the button to sit on top of your menu, you can do it like this.

$('a').click(function () {
    var effect = 'slide';
    var options = {
        direction: 'right'
    }
    var duration = 200;
    if ($('.menu').is(":hidden")) {
        var w = $(this).parent().width()*0.62 - $(this).width();
        $(this).animate({
            right: w
        }, duration);
    } else {
        $(this).animate({
            right: "5%"
        }, duration);
    }
    $('.menu').stop().toggle(effect, options, duration);
});

Upvotes: 2

Related Questions