0

I found this is exactly what I want: jQuery .scrollTop(); + animation

var body = $("html, body");
body.stop().animate({scrollTop:0}, 500, 'swing', function() { 
  alert("Finished animating");
});

But the problem as a comment mentioned in the question is that the trigger function would fire twice.

I realize that it's because $("html, body") get two elements binding this event.

However, I noticed that I can't just simply remove one because in Safari it's body that works while in Chrome it's html that works.

And I don't know what would happen on other devices that I could access.

So, is there any perfect solution for this that I don't need to fire twice the function?

AGamePlayer
  • 7,404
  • 19
  • 62
  • 119
  • You could try debouncing the function – Jorjon Jun 18 '18 at 04:48
  • 1
    Wow, just did some Google of debouncing and my first time to hear that concept. Totally cool. Thanks! – AGamePlayer Jun 18 '18 at 04:59
  • 2
    Use [`document.scrollingElement`](https://developer.mozilla.org/en-US/docs/Web/API/Document/scrollingElement) instead (polyfil: https://github.com/mathiasbynens/document.scrollingElement) – Andreas Jun 18 '18 at 05:01

0 Answers0