JQuery Slide Onclick

Posted by everreadyeddy on Stack Overflow See other posts from Stack Overflow or by everreadyeddy
Published on 2012-11-21T16:46:40Z Indexed on 2012/11/21 16:59 UTC
Read the original article Hit count: 179

Filed under:
|
|

I am using the code in example http://www.faridesign.net/2012/05/create-a-awesome-vertical-tabbed-content-area-using-css3-jquery/ I am trying to slide the div tags on a button click on the list so the current tab-content will slide in and the tab just clicked will slide out. I currently have the working example where I can switch between divs fine, but I need to slide in and out between divs. Is there any script I can do this with the current code. using .slide or .effect instead of .show() looks to display two divs at the same time. I'm not sure what I am doing wrong.

<div id="v-nav">
<ul>
    <li tab="tab1" class="first current">Main Screen</li>
    <li tab="tab2">Div 1</li>
    <li tab="tab3">Div 2</li>
    <li tab="tab4">Div 3</li>
    <li tab="tab5">Div 4</li>
    <li tab="tab6">Div 5</li>
    <li tab="tab7">Div 6</li>
    <li tab="tab8" class="last">Div 7</li>
</ul>
<div class="tab-content">
   <h4>Main Screen</h4>
</div>
<div class="tab-content">
    <h4>Div 1</h4>
</div>
<div class="tab-content">
    <h4>Div 2</h4>
</div>
<div class="tab-content">
    <h4>Div 3</h4>    
</div>
<div class="tab-content">
    <h4>Div 4</h4>                   
</div>
<div class="tab-content">
    <h4>Div 5</h4>                   
</div>
<div class="tab-content">
    <h4>Div 6</h4>                   
</div>
    <div class="tab-content">
    <h4>Div 7</h4>  
</div>

My Script looks like

$(function () {
var items = $('#v-nav>ul>li').each(function () {
    $(this).click(function () {
        //remove previous class and add it to clicked tab
        items.removeClass('current');
        $(this).addClass('current');

        //hide all content divs and show current one
        //$('#v-nav>div.tab-content').hide().eq(items.index($(this))).show();

        //$('#v-nav>div.tab-content').hide().eq(items.index($(this))).fadeIn(100);    

        $('#v-nav>div.tab-content').hide().eq(items.index($(this))).slideToggle();
        window.location.hash = $(this).attr('tab');
    });
});

if (location.hash) {
    showTab(location.hash);
}
else {
    showTab("tab1");
}

function showTab(tab) {
    $("#v-nav ul li:[tab*=" + tab + "]").click();
}

// Bind the event hashchange, using jquery-hashchange-plugin
$(window).hashchange(function () {
    showTab(location.hash.replace("#", ""));
})

// Trigger the event hashchange on page load, using jquery-hashchange-plugin
$(window).hashchange();

});

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery