简体   繁体   中英

Adjust jQuery accordion - open all tabs

I have developed a very basic Accordion with jQuery.

I'd like to allow all accordions to be open at any time.

I'd also like to have text that says open & close depending if they're open/close.

HTML:

<div id="accordion">
    <h4 class="accordion-toggle">Accordion 1</h4>
    <div class="accordion-content default">
        <p>Cras malesuada ultrices augue molestie risus.</p>
    </div>
    <h4 class="accordion-toggle">Accordion 2</h4>
    <div class="accordion-content">
        <p>Lorem ipsum dolor sit amet mauris eu turpis.</p>
    </div>
    <h4 class="accordion-toggle">Accordion 3</h4>
    <div class="accordion-content">
        <p>Vivamus facilisisnibh scelerisque laoreet.</p>
    </div>
</div>

CSS:

.accordion-toggle {cursor: pointer; margin: 0;}
.accordion-content {display: none;}
.accordion-content.default {display: block;}

Javascript:

$(document).ready(function($) {
    $('#accordion').find('.accordion-toggle').click(function(){

        //Expand or collapse this panel
        $(this).next().slideToggle('fast');

        //Hide the other panels
        $(".accordion-content").not($(this).next()).slideUp('fast');

    });
});

http://jsfiddle.net/zbjv3cak/

Here you are sir:

<a href="#" class="open">Open All</a>
    <a href="#" class="close">Close All</a>
    <div id="status"></div>
    <div id="accordion">
        <h4 class="accordion-toggle">Accordion 1</h4>
        <div class="accordion-content default">
            <p>Cras malesuada ultrices augue molestie risus.</p>
        </div>
        <h4 class="accordion-toggle">Accordion 2</h4>
        <div class="accordion-content">
            <p>Lorem ipsum dolor sit amet mauris eu turpis.</p>
        </div>
        <h4 class="accordion-toggle">Accordion 3</h4>
        <div class="accordion-content">
            <p>Vivamus facilisisnibh scelerisque laoreet.</p>
        </div>
    </div>

And The JS:

$(document).ready(function($) {
        $('.accordion-toggle').click(function(){

            //Expand or collapse this panel
            $(this).next().slideToggle('fast');

            //Hide the other panels
            $(".accordion-content").not($(this).next()).slideUp('fast');
        });

        $(".open").click(function(e){
        e.preventDefault();
        $('.accordion-toggle').next().slideDown('fast');
        $("#status").text("Opened");
        });

        $(".close").click(function(e){
        e.preventDefault();
        $('.accordion-toggle').next().slideUp('fast');
        $("#status").text("Closed");
        });
    });

http://jsfiddle.net/zbjv3cak/1/

To allow all of the accordion panels to be open at once, simply remove this line, since it collapses the other panels.

// Hide the other panels
$(".accordion-content").not($(this).next()).slideUp('fast');

To display the open/close status for each panel, you can add a callback function that runs after a panel is shown or hidden, and test for its new visibility using jQuery's .is()

 $(this).next().slideToggle('fast', function(){
   var status = $(this).is(':hidden') ? 'close' : 'open';
   $(this).next('.accordion-status').html(status);
 });

Note that you'll need to add an element to display this status for each panel (in this example, the DIV with class 'accordion-status' )

full Fiddle: http://jsfiddle.net/67w3pa89/

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