Using Bootstrap 2.3.2 I have an accordion with a single panel that is open when the page is loaded.
<div class="accordion" id="refine">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#refine" href="#refine-search">
Title
</a>
</div>
<div id="refine-search" class="accordion-body collapse in">
<div class="accordion-inner">
Test text....
</div>
</div>
</div>
The site is fully responsive. When switching to a mobile screen size [ @media (max-width: 979px) ] I want the accordion panel to close automatically, i.e. effectively I want the div refine-search line to change to "collapse out".
When in mobile mode, the accordion must still work, e.g. the user can click on the accordion heading and the panel will expand hence I do not want duplicate divs to turn the panel off using .hidden-desktop utility classes etc.
I've searched high and low for an answer - can anyone help?
Just add data-toggle="collapse" and a data-target to the element to automatically assign control of one or more collapsible elements. The data-target attribute accepts a CSS selector to apply the collapse to. Be sure to add the class collapse to the collapsible element.
open accordian > at right side go to accordian seting > scrol down to Expand/Collapse Accordion Option On Page Load > choose Hide/close All Accordion.. thanks.
Now, using the jQuery accordion() method, create the Accordion and keep the collapsible property as true. Set the active property to false To make the accordion collapse, by default.
Example Explained. The . collapse class indicates a collapsible element (a <div> in our example); this is the content that will be shown or hidden with a click of a button. To control (show/hide) the collapsible content, add the data-toggle="collapse" attribute to an <a> or a <button> element.
So I eventually figured out how to do this, posting it in case it's of help to anyone.
Alter the HTML to:
<div class="accordion" id="refine">
<div class="hidden-phone">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#refine" href="#refine-search">
<legend>
<h2>Refine your search</h2></legend>
</a>
</div>
</div>
<div class="visible-phone">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#refine" href="#refine-search">
<legend>
<h2>Refine your search (press to reveal)</h2></legend>
</a>
</div>
</div>
<div id="refine-search" class="accordion-body collapse in">
<div class="accordion-inner">
Test text....
</div>
</div>
</div>
And then use this JS in the file.
$(window).bind('resize load', function() {
if ($(this).width() < 767) {
$('#refine-search').removeClass('in');
$('#refine-search').addClass('out');
} else {
$('#refine-search').removeClass('out');
$('#refine-search').addClass('in');
}
});
For Bootstrap 3.x this worked great with no change to their example code:
$(window).bind('resize load', function() {
if ($(this).width() < 767) {
$('.collapse').removeClass('in');
$('.collapse').addClass('out');
} else {
$('.collapse').removeClass('out');
$('.collapse').addClass('in');
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With