jQuery scrollTop not working in Chrome but working in Firefox

后端 未结 15 1765
再見小時候
再見小時候 2020-11-29 02:54

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 w

15条回答
  •  失恋的感觉
    2020-11-29 03:52

    I have used this with success in Chrome, Firefox, and Safari. Haven't been able to test it in IE yet.

    if($(document).scrollTop() !=0){
        $('html, body').animate({ scrollTop: 0 }, 'fast');
    }
    

    The reason for the "if" statement is to check if the user is all ready at the top of the site. If so, don't do the animation. That way we don't have to worry so much about screen resolution.

    The reason I use $(document).scrollTop instead of ie. $('html,body') is cause Chrome always return 0 for some reason.

提交回复
热议问题