41

I'm trying to animate while scrolling but no luck with my code...

I have this jquery

$(window).scrollTop(200);

Now wanted to give animation effect

So tried these but not working:

1. $(window).animate({scrollTop:200},1000);
2. $('body').animate({scrollTop: 200}, 1000);

I have applied this in a click function like this:

$('.goto').click(function(){
    $(window).scrollTop(485); // its working
});

And now I want to give effect of animate but not working...

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231

6 Answers6

72

You have to use $('html,body') instead of $(window) because window does not have a scrollTop property.

$('#scroll-bottom').on('click', function() {
  $('html, body').animate({
    scrollTop: 2000
  }, 2000); // for all browsers
  
  // $('html').animate({scrollTop: 2000}, 2000); // works in Firefox and Chrome
  // $('body').animate({scrollTop: 2000}, 2000); // works in Safari
})
#top {
  margin-bottom: 2000px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="top">
  <button id="scroll-bottom">scroll</button>
</div>
<div>bottom</div>
Vucko
  • 20,555
  • 10
  • 56
  • 107
  • 3
    Yes, in _chrome_ and _safari_ works for `$('html,body')` and `$(body)`, but in _firefox_ works for `$('html,body')` and `$(html)`. Obviously the `scrolltop` has some browser compatibility issues. However, use `$('html,body')`, that seems to work for both. – Vucko Sep 13 '13 at 08:34
  • Anyone know of a solution for mobile devices as well? – JisuKim82 Apr 27 '15 at 22:49
  • What if you're in a modal/dialog box and you want to scroll to a specific part withing that section? Nvm: http://stackoverflow.com/questions/16072895/how-to-scroll-to-an-element-within-a-modal-using-jquery – eaglei22 Jan 23 '17 at 19:41
  • 1
    that's weird I'm using Chrome v67.0.3396.99 and $('body') is not working... I have to use $('html, body') – Mister Q Jul 13 '18 at 14:19
  • You saved me, I did `$('html')` and my client complained it is not working on safari. `$('html, body')` works great. – wonsuc May 24 '19 at 13:54
50

if you have html and body style height:100%; its not working use

height: auto;
min-height: 100%;
Michal Orlík
  • 501
  • 4
  • 3
3

I had this problem as well and realised that the problem was within the CSS.

In my case, I needed to remove the overflow-x: hidden; from the HTML tag.

Remove:

html {
    overflow-x: hidden;
}

Then, it worked.

Hope that helps someone!

Brad Ahrens
  • 4,864
  • 5
  • 36
  • 47
2

you just need to add pixel

$('body').animate({ scrollTop: "300px" }, 1000);
Kimtho6
  • 6,154
  • 9
  • 40
  • 56
1

DEMO

<html>
function scrollmetop(dest){
    var stop = $(dest).offset().top;
    var delay = 1000;
    $('body,html').animate({scrollTop: stop}, delay);
    return false;
}

scrollmetop('#test');
<body>
    <div style="margin: 100px 100px 1000px 100px">
       <div id="test" style="width: 100px; height: 100px; border: 3px solid black;">target object</div>
    </div>
</body>
</html>
coder
  • 13,002
  • 31
  • 112
  • 214
0

I'm using Angular and was trying to scroll down to an item that had been added in an ng-repeat. I put this code inside a $timeout (with zero time, just to make it happen after the elements displayed) and this was sufficient for the new item to have an offset().top...

...but I think there was just way too much going on adding dozens of new elements, so it didn't have the processing power to scroll-animate. When I set the timeout time to 1 second, it worked (though it actually took 7 seconds before the timeout got called).

I concluded that animated, smooth scrolling won't really be tractable here, and instead I'm just using

document.body.scrollTop = entry.offset().top
MalcolmOcean
  • 2,807
  • 2
  • 29
  • 38