Offset scrollTop animation

I am using jQuery UI accordion, but part of the text is too long, which leads to the fact that it is not the top of the page. What I wanted was when the section was open, it would move to the beginning of the section. This code works fine while doing this, but it snaps to the top, which looks clitchy.

$('#accordion').bind('click',function(){ theOffset = $(this).offset(); $(window).scrollTop(theOffset.top - 50); }); 

How would I animate scrollTop so that it "glides" at the top

Thank you very much

+4
source share
2 answers

Using

 $('body,html').animate({ scrollTop: theOffset.top - 50 }); 

instead

 $(window).scrollTop(theOffset.top - 50); 
+9
source

use jquery animimate to animate properties in a specified time, and not just apply them instantly.

 $('#accordion').bind('click',function(){ theOffset = $(this).offset(); $('body,html').animate({ scrollTop: theOffset.top - 50; }); }); 
0
source

All Articles