Xhynk
Xhynk

Reputation: 13840

Horizontally slide out content with JQuery on click, click again to close?

I can't seem to quite find what I need. Without trying too hard to explain it, I basically need the "Connect" social media bar in the top right of this website - http://www.cmssquirrel.com/web_works/

I won't be using it for that purpose exactly, but it works exactly like I need my function to do. Everything I found seems to be a vertical menu style thing. I need to horizontally expand content on click and hide it again on click once more. Not quite sure where to begin?

Upvotes: 3

Views: 6872

Answers (2)

shaunsantacruz
shaunsantacruz

Reputation: 8930

Here's more of a starting point than a polished solution, but it should get you going in the right direction.

DEMO

HTML

<div class="wrap">
    <a class="open" href="#">open</a>
    <div class="outer">
        <div class="slide">
            <a href="#">One</a>
            <a href="#">a</a>
            <a href="#">uaoeua</a>
            <a href="#">aoeue</a>
            <a href="#">aaoeeo</a>
        </div>
    </div>
</div>

jQuery

var w = 0;

$('.slide').children().each(function() {
    w += $(this).outerWidth();
});

$('.outer').width(w+5);
$('.wrap').width(w);
$('.slide').css('left', w);

$('.open').toggle(function() {
    $('.slide').stop().animate({
        left: 0
    });
    $(this).html('close');
}, function() {
    $('.slide').stop().animate({
        left: w
    });
    $(this).html('open');
});

CSS

.wrap {
    position: relative;
    left: 50px;
    top: 20px;
}
.outer {
    height: 40px;
    position: relative;
    overflow: hidden;
}
.slide {
    border-radius: 19px 19px 19px 19px;
    position: absolute;
    top: 0;
    right: 0;
    background-color: black;
    height: 40px;
}
a {
    color: gray;
    line-height: 35px;
    float: left;
    outline: none;
}
.slide a {
    float: left;
    display: block;
    padding: 0 10px;
}
.slide > :first-child {
    padding-left: 15px;
}
.slide > :last-child {
    padding-right: 45px;
}
.open {
    position: absolute;
    right: -25px;
    top: 0;
    background-color: black;
    border-radius: 19px 19px 19px 19px;
    height: 40px;
    padding: 0 15px;
    z-index: 5;
}

Upvotes: 4

conrad10781
conrad10781

Reputation: 2273

You can extend jQuery's animation functionality to achieve this.

See a demo of this at:

http://dock.ronggur.com/tutorial/jquery%20tutorial%20-%20horizontal%20animated%20menu/

which is outlined at:

http://sandbox.ronggur.com/2009/01/25/jquery-tutorial-horizontal-animated-menu/

Essentially, you could just substitute the hover event in that demo, and use a click event.

Upvotes: 1

Related Questions