jQuery .scrollTop(); + animation -
i set page scroll top when button clicked. first used if statement see if top of page not set 0. if it's not 0 animate page scroll top.
var body = $("body"); var top = body.scrolltop() // position of body if(top!=0) { body.animate({scrolltop:0}, '500'); }
the tricky part animating after page has scrolled top. next thought is, find out page position is. used console log find out.
console.log(top); // result 365
this gave me result of 365, i'm guessing position number @ before scrolling top.
my question how set position 0, can add animation runs once page @ 0?
thanks!
to this, can set callback function animate command execute after scroll animation has finished.
for example:
var body = $("html, body"); body.stop().animate({scrolltop:0}, 500, 'swing', function() { alert("finished animating"); });
where alert code is, can execute more javascript add in further animation.
also, 'swing' there set easing. check out http://api.jquery.com/animate/ more info.
Comments
Post a Comment