Thursday, October 17, 2013

jQuery UI accordion Scroll to top

When your jquery accordion has alot of content it tends to scroll the user toward the bottom of the page. If you want to scroll back to the top of the section they opened change your js to look like-a-so.

$(".accordion").accordion({
        activate: function(event, ui){
             var scrollTop = $(".accordion").scrollTop();
             var top = $(ui.newHeader).offset().top;
          //do magic to scroll the user to the correct location
          //works in IE, firefox chrome and safari
             $("html,body").animate({ scrollTop: scrollTop + top -35 }, "fast");
            },
         heightStyle: 'content' ,
        active: false,
         
    });


Stumble Upon CodePyro

3 comments:

Post a Comment