I'm have a bunch of accordions on a site built with Bootstrap. I'm trying to add a behavior where the page will scroll to the open panel once the accordion is done collapsing. It works in all browsers except IE (of course!). Here's the function I've written:
$('.accordion').on('hidden', function() {
$('.accordion .current').removeClass('current');
$(this).find('.in').prev().find('a').addClass('current');
$('html,body').animate({'scrollTop':$('.accordion-toggle.current').position().top},500);
})
IE says the position() property of the newly made "current" item is undefined. I know the object is there because I ran an alert on $('.current'), and it reported [object Object], but it seems to be unable to find the position of it. Does it have something to do with the execution queue of this function? Is it looking for the position of an object that doesn't exist yet? I'm tearing my hair out here!
HTML:
<div id="accordion" class="accordion">
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" href="#collapseOne" data-parent="#accordion" data-toggle="collapse">Panel 1</a>
</div>
<div id="collapseOne" class="accordion-body collapse">
<div class="accordion-inner">
<!-- Content goes here -->
</div>
</div>
</div>
</div>
@Kris Hollebeck...
I want it to scroll to the currently open pane. There is no automatic provision for that in Bootstrap. It does not add a class to the heading, but to the pane below it. I want the heading to be visible in the window, too. I may be going about it wrong, but that's what I'm trying to do and, at the risk of repeating myself, it works in every other browser except IE.
Here is a fiddle that shows what I am trying to do. Run it in IE and you will see that it does not work.