0

How do I instantly (not animated!) scroll to the top in Angular8+ from a service?
window.scrollto(0,0) does not work.

$([document.documentElement, document.body]).animate({
   scrollTop: 0
}, 100)

Isn't beautiful but works at all at least but the duration value seems to not be allowed to be set to 0 (which would anyway completely lose the purpose of jquery's animate method :) )

Martin Eckleben
  • 753
  • 8
  • 26
  • 1
    [This](https://stackoverflow.com/questions/48048299/angular-5-scroll-to-top-on-every-route-click/48048822) might help. Also make sure you have no `scroll-behavior: smooth;` in `body`, `html` or the container. – Harun Yilmaz Jul 03 '20 at 09:24
  • Aw man this was the problem scroll-behavior: smooth; at least for jquery animate with duration 0. I feel so endlessly stupid. Thank you a lot, a lot, a lot. Post it as an answer then I'll accept it? – Martin Eckleben Jul 03 '20 at 09:30
  • You're welcome. Glad to hear it worked for you :) No worries, btw. You can accept @Beller's answer as well. – Harun Yilmaz Jul 03 '20 at 09:37

1 Answers1

2

I always use scrollIntoView like this:

var element = document.getElementById("box");
element.scrollIntoView();

You can add a few optional params

element.scrollIntoView(false);
element.scrollIntoView({block: "end"});
element.scrollIntoView({behavior: "smooth", block: "end", inline: "nearest"});

Edit from OP:
Also make sure no scroll-behavior: smooth; is active!

Martin Eckleben
  • 753
  • 8
  • 26
Beller
  • 828
  • 1
  • 6
  • 19