Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scrolltop with animate not working

Tags:

jquery

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...

like image 507
Bhojendra Rauniyar Avatar asked Sep 13 '13 06:09

Bhojendra Rauniyar


People also ask

Why my scrollTop is not working?

If your CSS html element has the following overflow markup, scrollTop will not function. To allow scrollTop to scroll, modify your markup remove overflow markup from the html element and append to a body element.

What is $( window scrollTop ()?

jQuery scrollTop() Method The scrollTop() method sets or returns the vertical scrollbar position for the selected elements. Tip: When the scrollbar is on the top, the position is 0.


1 Answers

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>
like image 139
Vucko Avatar answered Nov 04 '22 10:11

Vucko