Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate jQuery scrolltop

I have a scrollTop function in jQuery but I can't animate it. Is it possible?

$(".loadmore").click(function() {
  $(this).toggleClass("up-arrow", 1000);
  $(window).scrollTop($('.docs').offset().top, 2000);
});
like image 850
George Avatar asked Nov 30 '22 23:11

George


2 Answers

You can use animate() for this.

Example code applied on a div is as follows :

//Scroll to bottom
$('div').animate({scrollTop: $('div').get(0).scrollHeight}, 3000);

//$('div').get(0).scrollHeight - will give the full height of div.
//scrollTop - will be used to animate from the current position to page end.
//3000 - will be the duration.

Demo can be found here : http://jsfiddle.net/codebombs/GjXzD/

like image 169
Sasidhar Vanga Avatar answered Dec 05 '22 19:12

Sasidhar Vanga


$('html, body').animate({ scrollTop: $('.docs').offset().top}, 2000);
like image 43
Ifeanyi Chukwu Avatar answered Dec 05 '22 17:12

Ifeanyi Chukwu