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:

Unknown said...

Hi,
Could you show me a working demo for this code, I think this is what I am after.
Thanks a lot

for IT the said...

great

Nannie Co Pam said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from JQuery Training in Chennai . or learn thru JQuery Training . or learn thru ES6 Online Training. Nowadays JavaScript has tons of job opportunities on various vertical industry.

Post a Comment