jQuery scrollTop() not working on 'body' element in Firefox

前端 未结 4 669
终归单人心
终归单人心 2021-01-01 15:04

I do not understand why the scrollTop() jquery function is not working on the \'body\' element on Firefox.

$(\'body\').scrollTop(0);

I fixe

相关标签:
4条回答
  • 2021-01-01 15:51

    Very Simple Code , working 100%

    $('body, html').scrollTop(0);
    
    0 讨论(0)
  • 2021-01-01 15:55

    Scroll

    $(window).scrollTop(0); seems to be supported by all browsers IE9+ (maybe IE8 but I don't test on that any more).

    Animated Scroll

    If you want to animate a scroll, jQuery returns an error if using the window object (1.11.2 tested). Instead, to animate a scroll, it's best to use both html and body to cover engines which utilise either one. So:

    $('html, body').animate({scrollTop:0},500); will scroll to the top of the browser in half a second.

    Scroll Position

    You cannot use $('html,body').scrollTop() to find the current scroll position of the page - at least Chrome doesn't support this (always returns 0). Instead, to consistently find the scroll position of a page, it's necessary to use $(window).scrollTop();.

    0 讨论(0)
  • 2021-01-01 15:55

    try this:

    your div to scroll:

    <div id="top"></div>
    

    and scroll top js:

    $('html,body').animate({scrollTop: $('#top').offset().top},'slow');
    
    0 讨论(0)
  • 2021-01-01 16:03

    Use window if you want consistency between browsers.

    $(window).scrollTop();
    
    0 讨论(0)
提交回复
热议问题