jQuery scrollTop not working in Chrome but working in Firefox

后端 未结 15 1676
再見小時候
再見小時候 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:54

    If it work all fine for Mozilla, with html,body selector, then there is a good chance that the problem is related to the overflow, if the overflow in html or body is set to auto, then this will cause chrome to not work well, cause when it is set to auto, scrollTop property on animate will not work, i don't know exactly why! but the solution is to omit the overflow, don't set it! that solved it for me! if you are setting it to auto, take it off!

    if you are setting it to hidden, then do as it is described in "user2971963" answer (ctrl+f to find it). hope this is useful!

    0 讨论(0)
  • 2020-11-29 03:56

    I had a same problem with scrolling in chrome. So i removed this lines of codes from my style file.

    html{height:100%;}
    body{height:100%;}
    

    Now i can play with scroll and it works:

    var pos = 500;
    $("html,body").animate({ scrollTop: pos }, "slow");
    
    0 讨论(0)
  • 2020-11-29 03:58

    maybe you mean top: 0

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

    from animate docs

    .animate( properties, [ duration ], [ easing ], [ callback ] )
    properties A map of CSS properties that the animation will move toward.
    ...

    or $(window).scrollTop() ?

    $('a#gotop').click(function() {
      $("html").animate({ top: $(window).scrollTop() }, "slow", function() { 
                                                                  alert('Animation complete.'); });
      //return false;
    });
    
    0 讨论(0)
提交回复
热议问题