What a pain in the ass it is to sign up for an account on this site..... I digress.
I was using the accordion as a mobile version of a website.... the accordion found @
http://jqueryui.com/accordion/. I had some long content and when opening a section, it would close the previously opened section simultaneously and you would end up somewhere in the middle, no where near the accordion heading. NO GOOD. There is very little on how to do this on the INTERWEBS so I thought I'd like to share. First, I tried to find a way to for the accordion to use scrollTo() but it would jump so i made a function that would animate the jump to the top of the page. All of this had to be done in the jQuery.ui file so you'll have to copy and modify it by doing this.
I copied the jQuery UI file and searched for the accordion function. I added this function to it:
function scrollTo(element, to, duration) {
if (duration < 0) return;
var difference = to - element.scrollTop;
var perTick = difference / duration * 10;
setTimeout(function() {
element.scrollTop = element.scrollTop + perTick;
scrollTo(element, to, duration - 10);
}, 10);
}
You can add this anywhere, so long as it's not inside any of the other functions. It needs to be by itself.
Search for the Accordion section of the UI and add the code in red to it....
_eventHandler: function( event ) {
var options = this.options,
active = this.active,
clicked = $( event.currentTarget ),
clickedIsActive = clicked[ 0 ] === active[ 0 ],
collapsing = clickedIsActive && options.collapsible,
toShow = collapsing ? $() : clicked.next(),
toHide = active.next(),
eventData = {
oldHeader: active,
oldPanel: toHide,
newHeader: collapsing ? $() : clicked,
newPanel: toShow
};
scrollTo(document.body, 0, 100); //Scroll to Top Modification
event.preventDefault();
.... and BOOM. I'm sure there are better ways to do this, and if i was more versed in jQuery, It would have been a lot easier, but this worked for me.