简体   繁体   中英

Jquery Accordion Automatic close

I'm working on creating a mobile accordion nav for a website. I have a basic accordion set up, the problem I am having is when I open one tab I want the other tabs to automatically close so only one tab can be opened at once. Here is the code

$(document).ready(function() {
// Collapsible Menu
function accordion(trigger) {
    //variables
    var $button = $(trigger),//trigger firing the event
        visible = true;//flag for wayfinding

        $button.hover().css({'cursor': 'pointer'});

    //event
    $button.click(function() {
        //conditional check
        if ( ! visible ) {
            $button.removeClass('active');
            $('.panel-title .icon').html('⊕');


            $(this).next().slideUp('slow',function() {
                $(this).addClass('visuallyhidden').slideDown(0);
                $('.panel-content').attr( 'aria-expanded','false' );
            });
        }else {
            $button.addClass('active');
            $('.panel-title.active .icon').html('⊗');

            $(this).next().slideUp(0,function() {
                $('.panel-content').attr( 'aria-expanded','true' );
                $(this).removeClass('visuallyhidden').slideDown('slow');
            });
        }

        //flag dude
        visible = !visible;
        return false
    });
}

//call to widget trigger1
accordion('#trigger1');
//call to widget trigger2
accordion('#trigger2');
//call to widget trigger3
accordion('#trigger3');

Codepen link - http://codepen.io/Ahhmmogh/pen/WvMMZN

Any help would be greatly appreciated.

Here's a working codepen: http://codepen.io/alezuc/pen/OVQvMV

I've added a check on panel's visibility and on 'active' title

if ( $(this).find('.panel-content').css('display') == 'none' ) {...}
if ( $(this).hasClass('active') ) {...}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM