I have a series of bootstrap 3 accordion panels with lots of content in them that are almost working... they load collapsed as wanted and open when clicked as they should, collapsing any other open panels in the process; all good...
but when I open Panel 1 and scroll down its content, and then open Panel 2, the Panel 2 content loads "upwards" past the top of the visible browser window; meaning the user has to scroll up to see the top of Panel 2. Frustrating!
I want Panel 2 to load so the top of it is visible within the browser window.
<div class="panel-group" id="accordion">
<!-- first panel -->
<div class="panel panel-default">
<div class="panel-heading">
<span class="strong">
<a data-toggle="collapse" data-parent="#accordion"
href="#collapseOne" id="predict">
Gettysburg <span class="caret"></span>
</a>
</span>
</div>
<div id="collapseOne" class="panel-collapse collapse">
<div class="panel-body">
<!--LOTS OF CONTENT - replaced with image-->
<img src="http://i.imgur.com/AMhADP1.png" />
</div>
</div>
</div>
<!-- second panel -->
<div class="panel panel-default">
<div class="panel-heading">
<span class="strong">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" id="aries">
Background <span class="caret"></span>
</a>
</span>
</div>
<div id="collapseTwo" class="panel-collapse collapse">
<div class="panel-body">
<!--LOTS OF CONTENT - replaced with image-->
<img src="http://imgur.com/j98Q0H8.png" />
</div>
</div>
</div>
</div>
Any ideas on how I can fix this, please?
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.
You can use use a link to collapse content. To do this, use the <a> tag with an href value of the ID of the content to collapse. On the collapsible content's container, add the . collapse class, and be sure to give it an ID.
To create an accordion that is collapsed by default, we need to set the 'active' property of the jQuery Accordion as false. Syntax: $("#demoAccordion"). accordion({ collapsible: true, active: false});
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.
The browser will always try to maintain its current position within the page, even when you collapse / show large elements that could disrupt that position.
You can solve this with a two part approach suggested by Gokhan:
The first part is pretty simple. From the Collapse Events Docs we'll find:
shown.bs.collapse - This event is fired when a collapse element has been made visible to the user (will wait for CSS transitions to complete).
We'll listen for this event like this:
$('#accordion').on('shown.bs.collapse', function (e) {
var id = $(e.target).prev().find("[id]")[0].id;
navigateToElement(id);
})
In the shown
event I've called a function navigateToElement
and passed in the id
from header of the visible panel. The navigate function will scroll to the id's position using jquery's animate:
function navigateToElement(id) {
$('html, body').animate({
scrollTop: $("#" + id).offset().top
}, 1000);
}
Here is another approach (similiar to @KyleMit).. find the open panel and use jQuery to scroll to the top of it:
$('#accordion').on('shown.bs.collapse', function () {
var panel = $(this).find('.in');
$('html, body').animate({
scrollTop: panel.offset().top
}, 500);
});
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