Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery scrollTop not working in Chrome but working in Firefox

I have used a scrollTop function in jQuery for navigating to top, but strangely 'the smooth animated scroll' stopped working in Safari and Chrome (scrolling without smooth animation) after I made some changes.

But it is still working smoothly in Firefox. What could be wrong?

Here is the jQuery function I used,

jQuery:

$('a#gotop').click(function() {     $("html").animate({ scrollTop: 0 }, "slow");     //alert('Animation complete.');     //return false; }); 

HTML

<a id="gotop" href="#">Go Top </a> 

CSS

#gotop {       cursor: pointer;       position: relative;       float: right;       right: 20px;       /*top:0px;*/ } 
like image 799
Maju Avatar asked Jun 15 '10 05:06

Maju


People also ask

Why is scrollTop 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.

What is Document body scrollTop?

scrollTop property gets or sets the number of pixels that an element's content is scrolled vertically. An element's scrollTop value is a measurement of the distance from the element's top to its topmost visible content. When an element's content does not generate a vertical scrollbar, then its scrollTop value is 0 .


1 Answers

Try using $("html,body").animate({ scrollTop: 0 }, "slow");

This works for me in chrome.

like image 100
Aaron Harun Avatar answered Dec 09 '22 09:12

Aaron Harun